পৃষ্ঠাসমূহ

Search Your Article

CS

 

Welcome to GoogleDG – your one-stop destination for free learning resources, guides, and digital tools.

At GoogleDG, we believe that knowledge should be accessible to everyone. Our mission is to provide readers with valuable ebooks, tutorials, and tech-related content that makes learning easier, faster, and more enjoyable.

What We Offer:

  • 📘 Free & Helpful Ebooks – covering education, technology, self-development, and more.

  • 💻 Step-by-Step Tutorials – practical guides on digital tools, apps, and software.

  • 🌐 Tech Updates & Tips – simplified information to keep you informed in the fast-changing digital world.

  • 🎯 Learning Support – resources designed to support students, professionals, and lifelong learners.

    Latest world News 

     

Our Vision

To create a digital knowledge hub where anyone, from beginners to advanced learners, can find trustworthy resources and grow their skills.

Why Choose Us?

✔ Simple explanations of complex topics
✔ 100% free access to resources
✔ Regularly updated content
✔ A community that values knowledge sharing

We are continuously working to expand our content library and provide readers with the most useful and relevant digital learning materials.

📩 If you’d like to connect, share feedback, or suggest topics, feel free to reach us through the Contact page.

Pageviews

Wednesday, March 22, 2017

Yii - Ad Hoc Validation

Sometimes you need to validate values that are not bound to any model. You can use the yii\base\DynamicModel class, which supports defining both attributes and rules on the fly.
Step 1 − Add the actionAdHocValidation method to the SiteController.

public function actionAdHocValidation() {
   $model = DynamicModel::validateData([
      '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 validate them.
Step 2 − Type http://localhost:8080/index.php?r=site/ad-hoc-validation 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 Validators

There are two types of custom validators −
  • Inline validators
  • Standalone validators
An inline validator is defined by a model method or an anonymous function. If an attribute fails the validation, you should call the yii\base\Model::addError() method to save the error message.
The following example of the RegistrationForm validates the city property, so it can accept only two values – London and Paris.
<?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 [
            ['city', 'validateCity']
         ];
      }
      public function validateCity($attribute, $params) {
         if (!in_array($this->$attribute, ['Paris', 'London'])) {
            $this->addError($attribute, 'The city must be either "London" or "Paris".');
         }
      }
   }
?>
A standalone validator extends the yii\validators\Validator class. To implement the validation logic, you should override the yii\validators\Validator::validateAttribute() method.
Step 1 − To implement the previous example using the standalone validator, add a CityValidator.php file to the components folder.
<?php
   namespace app\components;
   use yii\validators\Validator;
   class CityValidator extends Validator {
      public function validateAttribute($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 app\models;
   use app\components\CityValidator;
   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 [
            ['city', CityValidator::className()]
         ];
      }
   }
?>

No comments:

Post a Comment