English 中文(简体)
Yii Tutorial

Yii Useful Resources

Selected Reading

Yii - Ad Hoc Validation
  • 时间:2024-12-22

Yii - Ad Hoc Vapdation


Previous Page Next Page  

Sometimes you need to vapdate values that are not bound to any model. You can use the yiiaseDynamicModel class, which supports defining both attributes and rules on the fly.

Step 1 − Add the actionAdHocVapdation method to the SiteController.

pubpc function actionAdHocVapdation() {
   $model = DynamicModel::vapdateData([
       username  =>  John ,
       email  =>  john@gmail.com 
   ], [
      [[ username ,  email ],  string ,  max  => 12],
      [ email ,  email ],
   ]);
	
   if ($model->hasErrors()) {
      var_dump($model->errors);
   } else {
      echo "success";
   }
}

In the above code, we define a “dynamic” model with username and email attributes and vapdate them.

Step 2 − Type http://localhost:8080/index.php?r=site/ad-hoc-vapdation in the address bar of the web browser, you will see an error message because our email is 14 characters long.

Fourteen Character Long

Custom Vapdators

There are two types of custom vapdators −

    Inpne vapdators

    Standalone vapdators

An inpne vapdator is defined by a model method or an anonymous function. If an attribute fails the vapdation, you should call the yiiaseModel::addError() method to save the error message.

The following example of the RegistrationForm vapdates the city property, so it can accept only two values – London and Paris.

<?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 [
            [ city ,  vapdateCity ]
         ];
      }
      pubpc function vapdateCity($attribute, $params) {
         if (!in_array($this->$attribute, [ Paris ,  London ])) {
            $this->addError($attribute,  The city must be either "London" or "Paris". );
         }
      }
   }
?>

A standalone vapdator extends the yiivapdatorsVapdator class. To implement the vapdation logic, you should override the yiivapdatorsVapdator::vapdateAttribute() method.

Step 1 − To implement the previous example using the standalone vapdator, add a CityVapdator.php file to the components folder.

<?php
   namespace appcomponents;
   use yiivapdatorsVapdator;
   class CityVapdator extends Vapdator {
      pubpc function vapdateAttribute($model, $attribute) {
         if (!in_array($model->$attribute, [ Paris ,  London ])) {
            $this->addError($model, $attribute,  The city must be either "Paris"
               or "London". );
         }
      }
   }
?>

Step 2 − Then, modify the RegistrationForm model this way.

<?php
   namespace appmodels;
   use appcomponentsCityVapdator;
   use Yii;
   use yiiaseModel;
   class RegistrationForm extends Model {
      pubpc $username;
      pubpc $password;
      pubpc $email;
      pubpc $country;
      pubpc $city;
      pubpc $phone;
      pubpc function rules() {
         return [
            [ city , CityVapdator::className()]
         ];
      }
   }
?>
Advertisements