Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony Password Reset without FOS Bundle

Tags:

php

symfony

I'm trying to make a form that users can easily change their password. I hope my logic is correct, however I'm getting an error as follows;

Expected argument of type "string", "AppBundle\Form\ChangePasswordType" given

Here is my controller;

public function changePasswdAction(Request $request)
{
    $changePasswordModel = new ChangePassword();
    $form = $this->createForm(new ChangePasswordType(), $changePasswordModel);

    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        // perform some action,
        // such as encoding with MessageDigestPasswordEncoder and persist
        return $this->redirect($this->generateUrl('homepage'));
    }

    return $this->render(':security:changepassword.html.twig', array(
        'form' => $form->createView(),
    ));
}

Here is my model;

class ChangePassword
{

/**
 * @SecurityAssert\UserPassword(
 *     message = "Wrong value for your current password"
 * )
 */
protected $oldPassword;

/**
 * @Assert\Length(
 *     min = 6,
 *     minMessage = "Password should by at least 6 chars long"
 * )
 */
protected $newPassword;

/**
 * @return mixed
 */
public function getOldPassword()
{
    return $this->oldPassword;
}

/**
 * @param mixed $oldPassword
 */
public function setOldPassword($oldPassword)
{
    $this->oldPassword = $oldPassword;
}

/**
 * @return mixed
 */
public function getNewPassword()
{
    return $this->newPassword;
}

/**
 * @param mixed $newPassword
 */
public function setNewPassword($newPassword)
{
    $this->newPassword = $newPassword;
}   

}

Here is my change password type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class ChangePasswordType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('oldPassword', 'password');
        $builder->add('newPassword', 'repeated', array(
            'type' => 'password',
            'invalid_message' => 'The password fields must match.',
            'required' => true,
            'first_options'  => array('label' => 'Password'),
            'second_options' => array('label' => 'Repeat Password'),
        ));
    }

}

Here is my viewer;

{{ form_widget(form.current_password) }}
{{ form_widget(form.plainPassword.first) }}
{{ form_widget(form.plainPassword.second) }}

The solution mentioned by @dragoste worked well for me. I changed the following line

$form = $this->createForm(new ChangePasswordType(), $changePasswordModel);

with this line;

$form = $this->createForm(ChangePasswordType::class, $changePasswordModel);
like image 980
mergenc Avatar asked Oct 30 '22 03:10

mergenc


1 Answers

In recent Symfony releases you can pass only class name in createForm

Change

$form = $this->createForm(new ChangePasswordType(), $changePasswordModel);

to

$form = $this->createForm(ChangePasswordType::class, $changePasswordModel);

Read more about building forms at
http://symfony.com/doc/current/best_practices/forms.html#building-forms

like image 161
luchaninov Avatar answered Nov 15 '22 05:11

luchaninov