开发者

Yii : Custom Validation rule

开发者 https://www.devze.com 2023-03-28 00:38 出处:网络
I have two pages : Create , Update . both of have a field for upload img file 开发者_开发百科.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消