Yii外键的验证规则

And*_*rea 4 php validation yii yii-components

假设我有一个带有以下验证规则的ActiveRecord:

public function rules() {
    return array(
        array('model', 'required'),
        // ....
        array('model', 'exist',
            'allowEmpty' => false,
            'attributeName' => 'id',
            'className' => 'Model',
            'message' => 'The specified model does not exist.'
        )
    );
}
Run Code Online (Sandbox Code Playgroud)

第一个规则强制该model字段不为空,第二个规则检查它是否具有一致值(model是外键).

如果我尝试验证一个表格,我在其中留空了该字段,model我得到2个错误,一个用于第一个规则,一个用于第二个规则.

我只想收到" 不能为空 "的错误消息.

有没有办法在不满足第一条规则时停止验证?

boo*_*dev 10

你可以使用skipOnError:

return array(
    array('model', 'required'),
    // ....
    array('model', 'exist',
        'allowEmpty' => false,
        'attributeName' => 'id',
        'className' => 'Model',
        'message' => 'The specified model does not exist.',
        'skipOnError'=>true
    )
);
Run Code Online (Sandbox Code Playgroud)

编辑:

有人评论上述内容并不清楚,可能是因为这里的字段名称也是如此model.因此,在实施时请记住这一点.