假设我有一个具有以下验证规则的 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.'
)
);
}
第一条规则强制 model
字段不能为空,第二条规则检查它是否具有一致的值(model
是外键)。
如果我尝试验证我将字段 model
留空的表单,我会收到 2 个错误,一个针对第一条规则,一个针对第二条规则。
我只想收到“不能为空”错误消息。
有没有办法在不满足第一条规则时停止验证?
最佳答案
您可以使用 skipOnError
:
return array(
array('model', 'required'),
// ....
array('model', 'exist',
'allowEmpty' => false,
'attributeName' => 'id',
'className' => 'Model',
'message' => 'The specified model does not exist.',
'skipOnError'=>true
)
);
编辑:
上面有人评论说不清楚,可能是因为这里的field name也是model
。因此,在实现时请记住这一点。
关于php - Yii 外键验证规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12009122/