you should never trust the data received from users. to validate a model with user inputs, you should call yii\base\model::validate() method. it returns a boolean value if the validation succeeds. if there are errors, you may get them from the yii\base\model::$errors property.
using rules
to make the validate() function work, you should override the yii\base\model::rules() method.
step 1 − the rules() method returns an array in the following format.
[ // required, specifies which attributes should be validated ['attr1', 'attr2', ...], // required, specifies the type a rule. 'type_of_rule', // optional, defines in which scenario(s) this rule should be applied 'on' => ['scenario1', 'scenario2', ...], // optional, defines additional configurations 'property' => 'value', ... ]
for each rule, you should define at least which attributes the rule applies to and the type of rule applied.
the core validation rules are − boolean, captcha, compare, date, default, double, each, email, exist, file, filter, image, ip, in, integer, match, number, required, safe, string, trim, unique, url.
step 2 − create a new model in the models folder.
<?php
   namespace app\models;
   use yii;
   use yii\base\model;
   class registrationform extends model {
      public $username;
      public $password;
      public $email;
      public $country;
      public $city;
      public $phone;
      public function rules() {
         return [
            // the username, password, email, country, city, and phone attributes are
            //required
            [['username' ,'password', 'email', 'country', 'city', 'phone'], 'required'],
            // the email attribute should be a valid email address
            ['email', 'email'],
         ];
      }
   }
?>
we have declared the model for the registration form. the model has five properties − username, password, email, country, city, and phone. they are all required and the email property must be a valid email address.
step 3 − add the actionregistration method to the sitecontroller where we create a new registrationform model and pass it to a view.
public function actionregistration() {
   $model = new registrationform();
   return $this->render('registration', ['model' => $model]);
}
step 4 − add a view for our registration form. inside the views/site folder, create a file called registration.php with the following code.
<?php
   use yii\bootstrap\activeform;
   use yii\bootstrap\html;
?>
<div class = "row">
   <div class = "col-lg-5">
      <?php $form = activeform::begin(['id' => 'registration-form']); ?>
         <?= $form->field($model, 'username') ?>
         <?= $form->field($model, 'password')->passwordinput() ?>
         <?= $form->field($model, 'email')->input('email') ?>
         <?= $form->field($model, 'country') ?>
         <?= $form->field($model, 'city') ?>
         <?= $form->field($model, 'phone') ?>
         <div class = "form-group">
            <?= html::submitbutton('submit', ['class' => 'btn btn-primary',
               'name' => 'registration-button']) ?>
         </div>
      <?php activeform::end(); ?>
   </div>
</div>
we are using the activeform widget for displaying our registration form.
step 5 − if you go to the local host http://localhost:8080/index.php?r=site/registration and click the submit button, you will see validation rules in action.
step 6 − to customize the error message for the username property, modify the rules() method of the registrationform in the following way.
public function rules() {
   return [
      // the username, password, email, country, city, and phone attributes are required
      [['password', 'email', 'country', 'city', 'phone'], 'required'],
      ['username', 'required', 'message' => 'username is required'],
      // the email attribute should be a valid email address
      ['email', 'email'],
   ];
}
step 7 − go to the local host http://localhost:8080/index.php?r=site/registration and click the submit button. you will notice that the error message of the username property has changed.
step 8 − to customize the validation process, you may override these methods.
- 
yii\base\model::beforevalidate(): triggers a
yii\base\model::event_before_validate event.
 - 
yii\base\model::aftervalidate(): triggers a
yii\base\model::event_after_validate event.
 
step 9 − to trim the spaces around the country property and turn empty input of the city property into a null, you may the trim and default validators.
public function rules() {
   return [
      // the username, password, email, country, city, and phone attributes are required
      [['password', 'email', 'country', 'city', 'phone'], 'required'],
      ['username', 'required', 'message' => 'username is required'],
      ['country', 'trim'],
      ['city', 'default'],
      // the email attribute should be a valid email address
      ['email', 'email'],
   ];
}
step 10 − if an input is empty, you can set a default value for it.
public function rules() {
   return [
      ['city', 'default', 'value' => 'paris'],
   ];
}
if the city property is empty, then the default “paris” value will be used.