Form validation in yii 2 using model with example

Form validation in yii 2 using model with example
Reading Time: 4 minutes

Today we will learn how to validate user input using model. Form validation is always a time-consuming process but in Yii 2, It becomes simple and easy because Yii 2 has inbuilt validation method which can be used easily to check form validation. It made the developers task easy and time-saving.

Steps for form validation in Yii 2

  1. You need form first to apply form validation in Yii 2. Either you can write code manually or you can use Gii tool to generate CRUD. If you don’t know how to generate CRUD using gii then please read this article.
  2. I have created student form using CRUD. Below is the code.

    In the above form, I will apply these rules :-
    Name :- It will be required. It will contain letters.
    Roll No :- It will be required. It will contain numbers. It should be unique in the database.
    New Password :- It should be required at the time of creating the user. At the time of update, this field is not required. The password should match confirm password field. Password length should be eight characters long. It should contain at least one letter and one digit.
    Confirm Password :-
    It is equal to the password.
    Class :-
    It will string.
    Email :-
    It must be the valid email. It should be unique for each student.
    Website :-
    It should be valid website URL.
    Gender :-
    No rules
  3. To apply these rules in Yii 2. It is very simple. I am pasting my model code here and I will explain you each and everything.

    Let’s start understanding the above code. In the first line, we wrote fields name which we want to be required at the time of form creation. If we don’t write 'on'=>'create' then this put required every time we call this model. 'on'=>'create' is a scenario which we used here. To make this scenario work, we need to add one line code in StudentController.

    As you can see above, I have added $model->setScenario('create');  This will create a scenario and It will call only when we will be creating our form for the first time. Remember, the rules in which we do not specify scenario will run every time. You can read about custom scenarios by clicking here.
  4. In the second line, we have written name and roll no is required when update scenario will be called. So you have to write $model->setScenario('update'); in update action as we have written in creating action. It will apply update scenario in update action.
  5. In the next line, we have defined, roll number as the integer which will check roll no is the integer or not.
  6. In the next line, we have written roll no and email unique. It will check the if the roll no and email already exist in DB or not. If it exists then it will show error.
  7. Email validator will check if the email is valid email or not.
  8. compare will compare the field with the compareAttribute field. If both fields are equal then it will not give an error.
  9. In the next line, passwordCriteria rule is applied to the new password. This rule is defined in model only. we call it as inline validators. Read more here . You can see above code, I have added this action after rule action. The new password will call this rule to validate. In this, we have written the code which we have set for the password.
  10. In the next line, match function is used. It checks if the input value matches the specified regular expression.
  11. URL validator checks if this field is in URL format.
  12. In the next line, we do not want to perform any validation on these two, so we have written safely.
  13. We want to name and class to be the string then we have used string validator. It will not exceed  it’s length more than 50.
  14. In the last line also, we have applied string rule to all these and it will not exceed it’s length more than 255.
  15. So, we have covered all the validation in this. In this article, we have used some core validators and inline validators. Please read core validators here in details because I am not able to complete the whole list. Also, read about inline validators here. We have used one inline validator that is passwordCriteria. 
  16. When we will click on submit button then it will call the create action. In creating action we have this function $model->validate() which will perform validation and gives true if there is not error otherwise it will give error in $model->errors; function. We can collect all errors and display it to the users. This is all about form validation in Yii 2. If you face any issue while applying form validation please comment below.

2 Comments on “Form validation in yii 2 using model with example”

  1. If i wanted to validate the custom array field, that elements in array should not be empty or null. How to do that?

Leave a Reply

Your email address will not be published. Required fields are marked *