[英]Symfony Password Reset without FOS Bundle
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. @dragoste提到的解决方案对我来说效果很好。 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
在最近的Symfony版本中,您只能在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 http://symfony.com/doc/current/best_practices/forms.html#building-forms
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.