- Gii – Generating Module
- Gii – Generating Controller
- Gii – Creating a Model
- Yii - Gii
- Yii - Localization
- Yii - Authorization
- Yii - Authentication
- Yii - Error Handling
- Yii - Logging
- Yii - Aliases
- Yii - Fragment Caching
- Yii - Caching
- Yii - Testing
- Yii - Fields
- Yii - RESTful APIs in Action
- Yii - RESTful APIs
- Yii - Theming
- Yii - Database Migration
- Yii - Active Record
- Yii - Query Builder
- Yii - Data Access Objects
- Yii - Database Access
- Yii - Dependency Injection
- Yii - Configurations
- Yii - Creating a Behavior
- Yii - Behaviors
- Yii - Creating Event
- Yii - Events
- Yii - GridView Widget
- Yii - ListView Widget
- Yii - Data Widgets
- Yii - Data Providers
- Yii - Properties
- Yii - Sorting
- Yii - Pagination
- Yii - Formatting
- Yii - Files Upload
- Yii - Using Cookies
- Yii - Cookies
- Yii - Using Flash Data
- Yii - Sessions
- Yii - AJAX Validation
- Yii - Ad Hoc Validation
- Yii - Validation
- Yii - HTML Forms
- Yii - Rules of URL
- Yii - URL Routing
- Yii - URL Formats
- Yii - Responses
- Yii - HTTP Requests
- Yii - Creating Extensions
- Yii - Extensions
- Yii - Asset Conversion
- Yii - Assets
- Yii - Layouts
- Yii - Views
- Yii - Modules
- Yii - Widgets
- Yii - Models
- Yii - Using Actions
- Yii - Using Controllers
- Yii - Controllers
- Yii - Entry Scripts
- Yii - Application Structure
- Yii - Create Page
- Yii - Installation
- Yii - Overview
- Yii - Home
Yii Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Yii - Ad Hoc Vapdation
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.
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