Hi.
Yii guide explained that we can use validation in 3 form (method/predefined validator/and calss validate).
how we can use a calss validation can someone post a simple example here
Hi.
Yii guide explained that we can use validation in 3 form (method/predefined validator/and calss validate).
how we can use a calss validation can someone post a simple example here
First, write a validator class. You may refer to any class under /validators folder.
Then, declare a validation rule as follows,
array('attribute list', 'ClassName', ...name-value pairs to initialize validator...)
where ClassName refers to your validator. It can also be a path alias.
Quote
Then, declare a validation rule as follows,
array('attribute list', 'ClassName', ...name-value pairs to initialize validator...)
where ClassName refers to your validator. It can also be a path alias.
Hi qiang and others
I could not understand name-value pairs. what exactly should be pass as name-value pairs to validator calss assumes that our validator class is in protected\validator\user\register.php
array('username, password, password2, email, email2, url, address, pobox, telephone, cellphone, verifyCode, name, family',
'application.validators.user.register', ...name-value pairs to initialize validator...)
and next problem is in validator class, in user guid we have:
<?php class MyValidator extends CValidator
{
protected function validateAttribute($model,$attribute)
{
$value=$model->$attribute;
if($value has error)
$model->addError($attribute,$errorMessage);
}
}?> and it's a bit strange for me. I should check every attributes one by one with php hardcode or we can use somthing like
<?php return array(
array('username', 'length', 'min' => 3, 'max' => 100, 'on'=>'register'),
array('password','length', 'min' => 6, 'max' => 128, 'on'=>'register'),
array('password2','compare','compareAttribute'=>'password', 'on'=>'register'),
array('email', 'length', 'max' => 150, 'on'=>'register'),
array('email', 'email', 'on'=>'register'),
array('email2','compare','compareAttribute'=>'email', 'on'=>'register'),
array('url', 'length', 'max' => 250, 'on'=>'register'),
array('url','url', 'on'=>'register'),
array('name', 'length', 'max' => 150, 'on'=>'register'),
array('family', 'length', 'max' => 150, 'on'=>'register'),
array('address', 'length', 'max' => 250, 'on'=>'register'),
array('pobox', 'length', 'max' => 15, 'on'=>'register'),
array('telephone', 'length', 'max' => 30, 'on'=>'register'),
array('cellphone', 'length', 'max' => 30, 'on'=>'register'),
array('verifyCode', 'captcha', 'allowEmpty'=>!extension_loaded('gd')),
array('username, password, email, name, family', 'required', 'on'=>'register'),
);?>
I want do that just for reusing of code not for inventing wheel again!!!
Assume your new validator class has a property named 'errorMessage'. Then a name-value pair could be 'errorMessage'=>'some error message'. If you have multiple properties to be initialized, you can provide multiple pairs in the rule.
validateAttribute() only validates one attribute at a time. However, in a single rule, you can list several attributes to be validated.