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);
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With