English 中文(简体)
Yii Tutorial

Yii Useful Resources

Selected Reading

Yii - Validation
  • 时间:2024-11-03

Yii - Vapdation


Previous Page Next Page  

You should never trust the data received from users. To vapdate a model with user inputs, you should call yiiaseModel::vapdate() method. It returns a Boolean value if the vapdation succeeds. If there are errors, you may get them from the yiiaseModel::$errors property.

Using Rules

To make the vapdate() function work, you should override the yiiaseModel::rules() method.

Step 1 − The rules() method returns an array in the following format.

[
   // required, specifies which attributes should be vapdated
   [ attr1 ,  attr2 , ...],
   // required, specifies the type a rule.
    type_of_rule ,
   // optional, defines in which scenario(s) this rule should be appped
    on  => [ scenario1 ,  scenario2 , ...],
   // optional, defines additional configurations
    property  =>  value , ...
]

For each rule, you should define at least which attributes the rule apppes to and the type of rule appped.

The core vapdation 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 appmodels;
   use Yii;
   use yiiaseModel;
   class RegistrationForm extends Model {
      pubpc $username;
      pubpc $password;
      pubpc $email;
      pubpc $country;
      pubpc $city;
      pubpc $phone;
      pubpc 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 vapd 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 vapd email address.

Step 3 − Add the actionRegistration method to the SiteController where we create a new RegistrationForm model and pass it to a view.

pubpc 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 yiiootstrapActiveForm;
   use yiiootstrapHtml;
?>

<span class = "row">
   <span 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 ) ?>
         <span class = "form-group">
            <?= Html::submitButton( Submit , [ class  =>  btn btn-primary ,
                name  =>  registration-button ]) ?>
         </span>
      <?php ActiveForm::end(); ?>
   </span>
</span>

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 cpck the submit button, you will see vapdation rules in action.

Vapdation Rules

Step 6 − To customize the error message for the username property, modify the rules() method of the RegistrationForm in the following way.

pubpc 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 vapd email address
      [ email ,  email ],
   ];
}

Step 7 − Go to the local host http://localhost:8080/index.php?r=site/registration and cpck the submit button. You will notice that the error message of the username property has changed.

Change Username Property

Step 8 − To customize the vapdation process, you may override these methods.

    yiiaseModel::beforeVapdate(): triggers a

    yiiaseModel::EVENT_BEFORE_VALIDATE event.

    yiiaseModel::afterVapdate(): triggers a

    yiiaseModel::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 vapdators.

pubpc 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 vapd email address
      [ email ,  email ],
   ];
}

Step 10 − If an input is empty, you can set a default value for it.

pubpc function rules() {
   return [
      [ city ,  default ,  value  =>  Paris ],
   ];
}

If the city property is empty, then the default “Paris” value will be used.

Advertisements