Php Yii:自定义验证规则

Php Yii:自定义验证规则,php,yii,Php,Yii,我有两个页面:创建、更新。两者都有一个用于上传img文件的字段。 在创建页面时,我需要验证img文件,但在更新时,我不必验证。 如何为模型中的每个页面设置自定义验证 这可以通过使用场景来完成。 它们决定了何时应该使用特定的验证规则 比如: <?php class SomeModel extends CModel { public $image; // ... /** * Returns the validation rules for attribut

我有两个页面:创建、更新。两者都有一个用于上传img文件的字段。 在创建页面时,我需要验证img文件,但在更新时,我不必验证。
如何为模型中的每个页面设置自定义验证

这可以通过使用场景来完成。
它们决定了何时应该使用特定的验证规则

比如:

<?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
            )
        );
    }

}

这可以通过使用场景来完成。
它们决定了何时应该使用特定的验证规则

比如:

<?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
            )
        );
    }

}