[英]Yii Model rule for validation doesn't work
我是Yii的新手。 我已經創建了密碼更改表,頁面名稱已更改。 在該頁面中,有三個字段:舊密碼,新密碼和確認新密碼字段。
我的規則
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'),
);
}
調節器
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,
));
}
創建和更新頁面驗證工作。 我已經將其復制到更新頁面中,它工作正常。 我認為這是方案問題。 請幫助我找到解決方案。
嘗試一下...我認為Load模型會創建覆蓋您的方案的新模型。
$model=$this->loadModel($id);
$model->setScenario('change');
您必須將新密碼與確認密碼字段進行比較:
array('confirm_password', 'compare', 'compareAttribute' => 'new_password',
'message' => 'Password Must Be same.', 'on' => 'change'),
要么
array('new_password', 'compare', 'compareAttribute' => 'confirm_password',
'message' => 'Password Must Be same.', 'on' => 'change'),
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.