如果在Yii2中使用确认密码,则模型不会保存

时间:2016-11-03 15:43:15

标签: yii2 rules password-confirmation

我的项目我在使用确认密码时遇到问题。

我的数据库中没有确认密码字段,我认为这是我收到此错误的原因。

我的模特

     public $user_password_hash_repeat;
     public $agree;
     public $user_search;



//  const SCENARIO_LOGIN = 'login';
    const SCENARIO_REGISTER = 'signup';
    const SCENARIO_CREATE = 'create';
    const SCENARIO_UPDATE = 'update';
    const SCENARIO_PASSWORD = 'password';
    const SCENARIO_NEWPASSWORD = 'newpassword';


    public static function tableName()
    {
        return 'sim_user';
    }

    /** 
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['user_email', 'user_fname'], 'required', 'on' => 'update'], // on Update Method scenario
            [['user_email', 'user_fname', 'user_password_hash'], 'required', 'on' => 'create'], // on create method scenario
            [['user_fname','user_email','user_password_hash','user_password_hash_repeat'], 'required', 'on' => 'signup'], // on signup scenario
            [['user_password_hash'],'required' ,'on' => 'newpassword'],// repeat password scenario    
            ['agree','required','requiredValue' => 1,'message' => 'Tick the box', 'on' => 'signup'],// on signup scenario
            ['user_password_hash','match','pattern'=>'$\S*(?=\S*[a-z])(?=\S*[A-Z])(?=\S*[\d])\S*$','message'=>'Password must have atleast 1 uppercase and 1 number '],
            ['user_password_hash', 'string', 'min' => 6],
            ['user_password_hash_repeat', 'compare', 'compareAttribute'=>'user_password_hash', 'skipOnEmpty' => false],
            [['user_email'],'email'],
            [['user_company', 'user_suspended', 'user_deleted'], 'integer'],
            [['user_email'], 'string', 'max' =>255],
            ['user_email','unique','targetClass'=> '\app\models\SimUser','on'=> 'create'], //create method scenario for unique email
            ['user_email','unique','targetClass'=> '\app\models\SimUser','on'=> 'signup'],// signup method scenario for unique email
            ['user_email','unique','targetClass'=> '\app\models\SimUser','on'=> 'update'],//update method scenario for unique email
            [['user_fname', 'user_lname'], 'string', 'max' => 45],
            [['user_auth_key'], 'string', 'max' => 32],
            [['user_access_token'], 'string', 'max' => 100],
            ['user_email', 'exist', 'on' => self::SCENARIO_PASSWORD]


        ];
    }

控制器操作

public function actionSignup()
    {
        $company = new Company(); 
        $model = new SimUser(['scenario' => SimUser::SCENARIO_REGISTER]);
        if ($model->load(Yii::$app->request->post())&& $model->validate() && $company->load(Yii::$app->request->post())&& $company->validate()) {
           // var_dump($model); exit()
            $model->setPassword($model->user_password_hash);
            $model->generateAuthKey();      
           // $company->save();

            $model->company_id = 1; 
              try{
                    $model->save();
                }catch (Exception $ex) {
                    var_dump($ex->getMessage()); exit();
                }

            if ($model->save()){
                $auth = Yii::$app->authManager;
                $authorRole = $auth->getRole('staff');
                $auth->assign($authorRole, $model->user_id);
            }
          \Yii::$app->user->login($model);
          return $this->redirect(['/site/index']);  
        }

        return $this->render('signup', [
            'model' => $model,
            'company' => $company,

        ]);
    }

如果您看到规则,我将user_password_hash_repeat设置为skipOnEmpty = false,这会强制用户键入此字段。如果我将其设置为true并只输入密码并提交,则提交表单并成功保存模型。

我也尝试过使用user_password_hash_repeat所需,我最终无法保存模型。

我的规则设置有什么问题?

谢谢!

1 个答案:

答案 0 :(得分:0)

您似乎缺少load model的第二个参数,将空字符串作为第二个参数传递。

替换

$model->load(Yii::$app->request->post())

有了这个

$model->load(Yii::$app->request->post(), '') //Note: Pass second parameter as empty string

因此您的if条件看起来像

if ($model->load(Yii::$app->request->post(),'')&& $model->validate() && $company->load(Yii::$app->request->post(),'')&& $company->validate()) {
希望这有效。