Monday, 16 February 2015

Rules of Model in yii -- server side validations in YII

public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('field1,field2,field3', 'numerical', 'integerOnly'=>true),
array('field','in', 'range'=> range(1,13),'message'=>'Value should be between 1 to 13'),
                         array('field','match' ,'pattern'=> '/^[A-Za-z]+$/u','allowEmpty'=>true,'message'=> 'Numbers/Symbols should not be included A2-mandal'),
                        array('ward','compare', 'operator'=>'!=', 'compareValue'=>'0', 'message'=>'Ward not given'),
                       array('locality','match' ,'pattern'=> '/^[A-Za-z0-9]+$/u','allowEmpty'=>false, 'message'=> 'Locality empty or Special symbols given'),
                       array('field','in', 'range'=> array(1,2,3), 'message'=>'Value should be 1 or 2 or 3','on'=>'orphan'),
                     array('ostatus','compare', 'operator'=>'!=', 'compareValue'=>'', 'message'=>'Status of Orphan should be specified','on'=>'orphan'),
/* To apply this rule the scenario to be defined in controller to the actions to which this rule to be defined add these lines as 
if($model->orphan=== 'Y')
$model->scenario='orphan';    

   then the rule is applied 
*/              
                       array('STATUS','compare','operator'=>'!=','compareValue'=>'pending','on'=>'update'),
 /* The above rule is applicable in update action only. For this action there is no need to specify scenario */

                      array('fields', 'length', 'max'=>150),

}


                        

No comments:

Post a Comment