I am trying to create a form with which users can easily change their password. I hope my logic is correct, however I get the error message 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()) {
return $this->redirect($this->generateUrl('homepage'));
}
return $this->render(':security:changepassword.html.twig', array(
'form' => $form->createView(),
));
}
Here is my model;
class ChangePassword
{
protected $oldPassword;
protected $newPassword;
public function getOldPassword()
{
return $this->oldPassword;
}
public function setOldPassword($oldPassword)
{
$this->oldPassword = $oldPassword;
}
public function getNewPassword()
{
return $this->newPassword;
}
public function setNewPassword($newPassword)
{
$this->newPassword = $newPassword;
}
}
Here is my type of password to change;
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);