Search code examples
yii2yii2-basic-appyii2-useryii2-validation

Yii2 Confirm Password not working


I am trying to build a signup form, it works fine when I dont use repeat password, it saves to the model, but when I use my repeat password it just does'nt let me save them in the database. My code is below.

SimUser.php file

public function rules()
    {
        return [
            [['user_email', 'user_password_hash','company_id','user_fname', 'user_lname','agree'], 'required'],
            ['agree','required','requiredValue' => 1, 'message' => ''],
            ['user_password_hash_repeat', 'compare','compareAttribute' => 'user_password_hash','message' => 'Password don\'t match'],
            [['agree'],'safe'],
            [['user_password_hash_repeat'],'safe'],
            [['user_company', 'user_suspended', 'user_deleted'], 'integer'],
            [['user_created'], 'safe'],
            [['user_email'], 'string', 'max' => 255],
            [['user_password_hash'], 'string', 'max' => 72],
            [['user_fname', 'user_lname'], 'string', 'max' => 45],
            [['user_auth_key'], 'string', 'max' => 32],
            [['user_access_token'], 'string', 'max' => 100],
        ];
    }

My controller action: site/signup

 public function actionSignup()
    {
        $company = new Company(); 
        $company->load(Yii::$app->request->post());
        $company->save();


        $model = new SimUser();
        if ($model->load(Yii::$app->request->post())) {
            $model->setPassword($model->user_password_hash);
            $model->generateAuthKey();
            $model->company_id =  $company->company_id;
            //var_dump($model); exit();
            $model->save();
            $model = new LoginForm();
            return $this->render('login',[
                'model' => $model,
            ]);
        }
        return $this->render('signup', [
            'model' => $model,
            'company' => $company,
        ]);
    }

Here I am saving the comany name in the company model and the others in the user table.

My views file: signup.php

<h1>Sign Up</h1> 
                <?= $form->field($model, 'user_fname')->textInput(['placeholder'=>'First Name*','class'=>'form-control col-lg-4'])->label(false);?>
                <?= $form->field($model, 'user_lname')->textInput(['placeholder'=>'Last Name*','class'=>'form-control col-lg-4'])->label(false); ?>
                <?= $form->field($model,'user_email')->textInput(['placeholder'=>'Email*','class'=>'form-control col-lg-4'])->label(false); ?>
                <?= $form->field($model, 'user_password_hash')->passwordInput(['placeholder'=>'Password'])->label(false); ?>
                <?= $form->field($model, 'user_password_hash_repeat')->passwordInput(['placeholder'=>'Confirm Password*','class'=>'form-control col-lg-4'])->label(false); ?>
                <?= $form->field($company, 'company_name')->textInput(['placeholder'=>'Company Name*','class'=>'form-control col-lg-4'])->label(false); ?>
                <?php echo $form->field($model, 'agree')->checkbox(); ?>
                <div class="form-group"> 
                    <?= Html::submitButton('Sign Up', ['class' => 'pull-left padding-0 btn btn-success', 'name' => 'signup-button']) ?>
                </div>

The error message for the company doesn't go, it is always there.. attached output as image click here Can anyone of you help me solve this issue? Thanks in advance!!

Company.php */

public function rules()
{
    return [
        [['company_name'], 'required'],
        [['company_name'], 'string', 'max' => 75],
    ];
}

Solution

  • In the controller have saved Company in the beginnig itself. So everytime, I load the page It is saving before I could submit it. Hence the error remains as it is.

    Code to be changed:

    $company = new Company(); 
            $model = new SimUser(['scenario' => SimUser::SCENARIO_REGISTER]);
            if ($model->load(Yii::$app->request->post()) && $company->load(Yii::$app->request->post())) {
    
                $company->save();
    
                $model->setPassword($model->user_password_hash);
                $model->generateAuthKey();
                $model->company_id =  $company->company_id;            
                $model->save();
    

    So 1 issue has been solved.. Unable to solve the confirm password issue! Would be helpful if someone can post what the mistake is?