[英]Symfony Password Reset without FOS Bundle
我正在嘗試制作一個用戶可以輕松更改密碼的表單。 我希望我的邏輯是正確的,但是我得到的錯誤如下;
Expected argument of type "string", "AppBundle\Form\ChangePasswordType" given
這是我的控制器;
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(),
));
}
這是我的模特;
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;
}
}
這是我的更改密碼類型;
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'),
));
}
}
這是我的觀眾;
{{ form_widget(form.current_password) }}
{{ form_widget(form.plainPassword.first) }}
{{ form_widget(form.plainPassword.second) }}
@dragoste提到的解決方案對我來說效果很好。 我更改了以下行
$form = $this->createForm(new ChangePasswordType(), $changePasswordModel);
用這條線;
$form = $this->createForm(ChangePasswordType::class, $changePasswordModel);
在最近的Symfony版本中,您只能在createForm
傳遞類名
更改
$form = $this->createForm(new ChangePasswordType(), $changePasswordModel);
至
$form = $this->createForm(ChangePasswordType::class, $changePasswordModel);
閱讀更多關於建築形式的信息
http://symfony.com/doc/current/best_practices/forms.html#building-forms
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.