是否可以这样做。
我正在使用Yii 1.1
public function rules()
{
return array(
array('user_firstname, user_lastname, user_username, user_password,user_mobile,user_email', 'required','on'=>'createuser'),
if ($this->user_role > 2 )
{
array('user_special_permission, 'required'),
}
array('user_email, user_username, user_password', 'length', 'max'=>255),
array('user_active, user_deleted', 'length', 'max'=>1),
// The following rule is used by search().
// @todo Please remove those attributes that should not be searched.
array('user_id, user_firstname, user_lastname, user_mobile, user_email, user_username, user_password, user_last_login, user_num_logins, user_num_failed_logins, user_active, user_deleted', 'safe', 'on'=>'search'),
);
}
仅在user_special_permission
required
user_role >2
答案 0 :(得分:1)
对于Yii 1.1,您可以在Model类中实现自定义验证规则,如下所示:
在rules
数组中添加:
array('user_special_permission, 'role_validation'),
然后创建一个函数role_validation
:
public function role_validation($attribute, $params)
{
if ($this->user_role > 2 && empty($this->user_special_permission)) {
$this->addError('user_special_permission','Special Permission required.');
}
}
您可以调整显示错误消息的属性,而不是user_special_permission
,例如user_role
有关更多文档,请参阅Declaring Validation Rules。
答案 1 :(得分:0)
是的,在yii 1中是可能的 你可以使用过滤器。代码将是
array('user_special_permission, 'required','when'=>function($model){
return $model->user_role>2;
}
),