I have two pages : Create , Update . both of have a field for upload img file 开发者_开发百科. in creation of page i need to validate the img file , but in update i dont have to . how i can set custom validation for every page in the model ?
This can be done by using scenarios.
These determine when a particular validation rule should be used.
Something like :
<?php
class SomeModel extends CModel
{
public $image;
// ...
/**
* Returns the validation rules for attributes.
*/
public function rules()
{
return array(
array(
'image', // Attribute list
'image_validator', // Validation rule
'on' => 'update', // Scenarios when the validation rule should be used
'message' => 'The image is invalid!', // Error message
)
);
}
}
You can take a look at the official documentation for more information on validation/scenarios.
精彩评论