gpt4 book ai didi

php - 没有 FOS bundle 的 Symfony 密码重置

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:43:49 24 4
gpt4 key购买 nike

我正在尝试制作一个用户可以轻松更改密码的表单。我希望我的逻辑是正确的,但是我收到如下错误;

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

这是我的 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(),
));
}

这是我的模型;

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

关于php - 没有 FOS bundle 的 Symfony 密码重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37169507/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com