D
D
Doniyor Mamatkulov2017-09-13 20:40:35
Yii
Doniyor Mamatkulov, 2017-09-13 20:40:35

Validation error on "password repeat"?

Hello! I get a validation error when registering a user: "Repeat password is required" Although I enter the password in both fields without errors!
Controller (save data):

if($model->load(Yii::$app->request->post()) && $model->validate()) {
            $new = new Users();
            $new->name = $model->name;
            $new->lastname = $model->lastname;
            $p_number = str_replace(')','',str_replace('(','',str_replace(' ','',$model->phone_number)));
            $new->phone_number = $p_number;
            $new->password = Yii::$app->security->generatePasswordHash($model->password);
            $new->object = rand(1111111111, 9999999999);
            $conf = rand(1111, 9999);
            $new->confirmation = $conf;
            if($new->save()) {
                \Yii::$app->session->setFlash('success', 'Account successfully created');
                return $this->redirect(['/user/confirmation', 'object' => $new->object]);
            } else {
                \Yii::$app->session->setFlash('danger', 'Error creating account');
                return $this->redirect('register');
            }
        }

Validation rules in the model:
public $password2

public function rules()
    {
        return [
            [['name', 'phone_number', 'password', 'password2'], 'required'],
            ['phone_number', 'unique'],
            ['password2', 'compare', 'compareAttribute'=>'password', 'message'=> Yii::t('app', 'Passwords don\'t match')],
            [['password', 'password2'], 'string', 'min'=>6],
            [['alive'], 'integer'],
            [['name', 'lastname', 'phone_number', 'password'], 'string', 'max' => 255],
            [['object', 'confirmation'], 'safe']
        ];
    }

Displaying the password repeat field in the view:
<?= $form->field($model, 'password2')->passwordInput(['value'=>''])->label(Yii::t('app', 'Password repeat')) ?>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question