1

can you help me with this? I did read the docu but the rule is still not working. Did I done it wrong? I still searching for some examples but still nothing happened. I want to validate a user's password and repeat_password if they match.

this is my code in model. that two are declared as required too on that scenario.

[['password','password_repeat'], 'checkPassword', 'on'=>'create'],

the scenario is declared on actionCreate too.

public function checkPassword($attributes, $params)
    {
        if(!$this->password_repeat === $this->password)
        {
           $this->addError($attribute, 'Passwords do not match!');
        }
    }

I did try using compare validation on that two fields but it won't satisfy me. If you only compare one field, it won't validate the other field. if you validate them both, there's still a possibility that they contrast each other when you delete what you have entered on one of the two fields. So I want to create one that even you delete what you have enter on that two fields, who goes first or not, they will be validated. sorry for the trouble. I hope you still get what I want to happens. Thanks.

PS, I don't want to use a password validator widget(?). thank you.

3
  • Hint: in your code you have a parameter $attributes which should be $attribute. Commented Sep 29, 2015 at 6:26
  • @robsch still nothing, tho. it didn't trigger the validation Commented Sep 29, 2015 at 6:51
  • 1
    Your condition is wrong ! it should be $this->password_repeat !== $this->password Commented Sep 29, 2015 at 6:56

3 Answers 3

2

You Just Use This Validation in your model file like...

['password', 'compare', 'compareAttribute'=>'password_repeat',  'message' => 'Your error message'],
Sign up to request clarification or add additional context in comments.

4 Comments

can I use that with when validator? I'm still unsatisfied with compare validator. @bvp
you just use your scenario in controller file
$model->scenario = 'create';
$model= new Users(['scenario' => 'create']); this is the code in my controller
0

In your code.

$this->addError($attribute, 'Passwords do not match!');

Check your varname $attribute, it does not exist in your function declaration.

public function checkPassword($attributes, $params)

AddError can't assign the error to a form-field.

Comments

0

Try it

<?php $form = ActiveForm::begin(['enableAjaxValidation' => true]); ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.