它没有验证我的表单数据

时间:2013-11-27 10:45:58

标签: php yii

我有这样的形式

我的表单

  <?php

   $error = $model->getErrors();
   print_r($error);

   <form method="post" action="<?php echo Yii::app()->getBaseUrl(true).'/index.php/admin/user/resetpassword'?>" enctype="multipart/form-data">
     <div class="row">
       <label>Old Password</label><input type="password" name="oldpassword" value="<?php echo $model->email; ?>"/><span><?php if(isset($error['email'])) echo $error['email'][0]; ?></span>
    </div> 
    <div class="row">
      <label>New Password</label><input type="password" name="newpassword"/><span><?php if(isset($error['password'])) echo $error['password'][0]; ?></span>
    </div>
    <div class="row">
      <label>Repeat Password</label><input type="password" name="repeatpassword" value="<?php echo $model->employeeid; ?>"/><span><?php if(isset($error['employeeid'])) echo $error['employeeid'][0]; ?></span>
    </div>
    <?php echo CHtml::submitButton($model->isNewRecord ? 'Reset Password' : 'Save'); ?>
 </form>

我的模型类是

   class User extends CActiveRecord
   {
    public $oldpassword;
    public $newpassword;
    public $repeatpassword;
    /**
 * @return string the associated database table name
 */
public function tableName()
{
    return 'user';
}
    /**
 * @return array validation rules for model attributes.
 */
public function rules()
{
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    return array(
        array('email, password, employeeid, designation, manager, profilepic', 'required','except'=>'resetpassword'),
                   array('email','email'),              array('profilepic','file','types'=>'jpg,jpeg,png','allowEmpty'=>true,'on'=>'update'),
                    array('email,employeeid','unique'),
                    array('newpassword, repeatpassword, oldpassword','required','on'=>'resetpassword'),
                    array('repeatpassword','compare','compareAttribute'=>'newpassword','on'=>'resetpassword'),

        array('email, password, employeeid, designation, manager, profilepic', 'safe', 'on'=>'search'),
    );
}

我的控制器功能

     public function actionResetPassword()
    {
        $model = new User('resetpassword');

        if(Yii::app()->request->isPostRequest)
        {
            if($model->validate())
                $this->redirect(array('resetpassword','msg'=>'Password successfully changed..'));
        }
        $this->render('resetpassword',array('model'=>$model));
    }

即使已输入值,它总是会给出错误"oldpassword,repeatpassword,newpassword required" ..

有人可以帮我吗

提前致谢

2 个答案:

答案 0 :(得分:3)

这是因为您没有将表单值分配给模型变量......

在您的控制器中尝试此操作

   if(Yii::app()->request->isPostRequest)
        {
            $model->oldpassword=$_POST['oldpassword']; //will set to model variable
            $model->newpassword=$_POST['newpassword'];
            $model->repeatpassword=$_POST['repeatpassword'];
            if($model->validate())
                $this->redirect(array('resetpassword','msg'=>'Password successfully changed..'));
        }

希望它可以解决您的问题

答案 1 :(得分:0)

如上面的Kalpit所述,您没有为表单实例分配表单输入。 您也可以像这样解决这个问题..

$model = new User;
if(isset($_POST['resetpassword']))
{
    $model->attributes=$_POST['resetpassword'];
    if($model->save())
   // successfully saved, do something here
}