Tuesday, July 3, 2012

All about validation in Yii - PART1

Validation in YII (Server side & Client Side)- PART 1

Yii support server side validation &  client side validation and it has wide range of built in validators, here we are going to see the each and every points about validation features that Yii provide.

Declaring Validation Rules

We specify the validation rules in Model class through the rules() method which should return an array of rule configurations. validators are called in order if we specified more than one validator for attribute
rules() method is a default method that triggers yii validation by using rules() method while validate() called. validate() function triggered automatically while we call $model->save() orcall $model->validate().

Validation rule syntax

array('AttributeList', 'Validator', 'on'=>'ScenarioList'...), 

Here AttributeList defines the list of model attributes, if more than one attribute it should be separated by comma.
Validator defines type of validation to be done yii provides the built in validators for email, url and so on refer table 1 for built in validators.
ScenarioList tells on which scenario the validation rule should fire, more than one scenario will be separated by comma.

Adding validations to model

        public function rules(){
        return array('username','required');

the above validation rule tells the yii to validate username fields to be mandatory through required validator. we apply same validation rule for  more than one field, by separating them with comma
        public function rules(){
        return array('username,password','required');

in the above example we set username and password field to be mandatory and we can apply more than validation to same attribute
        public function rules(){
        return array(
        array('username','length','min'=>3, 'max'=>12);        

Above example tells yii to validate username attribute length should have 3-12 and it is required.

Yii triggers validation when we call $model->validate(); or $model->save(); While saving records validation automatically called by default, if validation should be avoided in save methods $model->save(false); //which cancels validation

Clear steps

Model Finally model looks likes this
        class LoginForm extends CFormModel
        public $username;
        public $password;        
        public function rules()
        return array(
        array('username, password', 'required'),
        array('username','length','min'=>3, 'max'=>12);

$model = new Users();

Trigger Validation

to get particular field validation error, Yii add these statements to view by default why we scaffolding our code.

<?php echo $form->error($model, 'username'); ?> 
To get error summary for all fields
<?php echo $form->errorSummary($model); ?>

Contd.. on PART II


Bangalore Web Guru said...

The blog was absolutely fantastic! Lot of great information which can be helpful in some or the other way. Keep updating the blog, looking forward for more contents...Great job, keep it up..Web Designing Bangalore | Website Designing Bangalore

Netguru Solution India Pvt. Ltd. said...

Netguru Solution India Pvt. Ltd. is one of the leading Website Design and Mobile Application Development Company in Pune.
Website Design Company In Pune
Web Design Company In Pune
Website Development Company In Pune
Mobile Application Development Company In Pune
Android App Development Company In Pune
iOS App Development Company In Pune

Jackie Co Kad said...

I have read your blog its very attractive and impressive. I like it your blog.

PHP 7 Training in chennai | PHP 7 Training Course

PHP 7 Training in chennai | Online PHP 7 Course

SARA MARK said...

Thank You for sharing and its interesting to know new things, Great.
Language Translation Services ,
Subtitling Companies in Bangalore