[英]Yii Model rule for validation doesn't work
I am new to Yii. 我是Yii的新手。 I have created password changing form, page name is change.
我已经创建了密码更改表,页面名称已更改。 In that page there are three fields old password, new password and confirm new password fields.
在该页面中,有三个字段:旧密码,新密码和确认新密码字段。
My rule 我的规则
public function rules() {
return array(
array('old_password, confirm_password, str_user_password', 'required',
'on' => 'change'),
array('confirm_password', 'compare', 'compareAttribute' => 'str_user_password',
'message' => 'Password Must Be same.', 'on' => 'change'),
);
}
Controller 调节器
public function actionChange($id)
{
$model=new User('change');
//$model->setScenario('change');
$model=$this->loadModel($id);
if(isset($_POST['User']))
{
$model->attributes=$_POST['User'];
if($model->save())
$this->redirect(array('view','id'=>$model->int_user_id));
}
$this->render('change',array(
'model'=>$model,
));
}
create and update page validation work. 创建和更新页面验证工作。 I have copied it into update page it is working perfectly.
我已经将其复制到更新页面中,它工作正常。 I think it's scenario problem.
我认为这是方案问题。 Please help me to find a solution.
请帮助我找到解决方案。
Try this... I think Load model create new model that override your scenario. 尝试一下...我认为Load模型会创建覆盖您的方案的新模型。
$model=$this->loadModel($id);
$model->setScenario('change');
You must compare new password with confirm password fields: 您必须将新密码与确认密码字段进行比较:
array('confirm_password', 'compare', 'compareAttribute' => 'new_password',
'message' => 'Password Must Be same.', 'on' => 'change'),
or 要么
array('new_password', 'compare', 'compareAttribute' => 'confirm_password',
'message' => 'Password Must Be same.', 'on' => 'change'),
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.