H
H
hiimnotwordy2018-05-23 20:05:58
symfony
hiimnotwordy, 2018-05-23 20:05:58

Why does isPasswordValid() work differently after form submission in Symfony 4 than before submission?

I can’t figure out why before sending everything works as it should, and after sending, the same logic stops working.

public function changePassword(Request $request, UserPasswordEncoderInterface $encoder)
    {

        $plainPassword = 'Test1234';

        # Working -- echo 1
        if ($encoder->isPasswordValid( $this->getUser(), $plainPassword)) {
            echo '1 Current password true'; # Password in data base: Test1234
        } else {
            echo '2 Current password wrong';
        }


        $form = $this->createForm(ChangePasswordType::class, $this->getUser());
        $form->handleRequest($request);

        if ($form->isSubmitted()) {

            # When submit form -- not working -- echo 2
            if ($encoder->isPasswordValid( $this->getUser(), $plainPassword)) {
                echo '1 Current password true';
            } else {
                echo '2 Current password wrong';
            }
            die;

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
voronkovich, 2018-05-23
@voronkovich

When you made a call to $form->handleRequest($request) the form mapped the data that came in the request by making a call to $user->setPassword().
The User entity now has a new password other than $plainPassword.

H
hiimnotwordy, 2018-05-23
@hiimnotwordy

Thanks for the answer. I'm trying to implement a user password check when changing the password, but in this case I don't understand how to compare the current password in the database after submitting the form. After the request, it turns out that we need to get the User object from the database again and compare it with the User from the form?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question