我正在使用ResetPassword
系统,这是用户收到电子邮件后在其中输入新密码的页面。
基本上我想做的就是在视图页面中分离RepeatedType
!
在视图(.twig)中,我有:
{{ form_start(resetForm) }}
{{ form_widget(resetForm.plainPassword) }}
<button>Reset password</button>
{{ form_end(resetForm) }}
代替
{{ form_widget(resetForm.plainPassword) }}
我想将它们分开并做类似的事情:
{{ form_widget(resetForm.plainPassword[1]) }}
{{ form_widget(resetForm.plainPassword[2]) }}
那可能吗 ?如果是,正确的语法是什么?
这是ChangePasswordFormType.php
的代码:
class ChangePasswordFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('plainPassword', RepeatedType::class, [
'type' => PasswordType::class,
'first_options' => [
'attr' => [
'class' => 'form-control form-control-sm',
],
'constraints' => [
new NotBlank([
'message' => 'Please enter a password',
]),
new Length([
'min' => 6,
'minMessage' => 'Your password should be at least {{ limit }} characters',
// max length allowed by Symfony for security reasons
'max' => 4096,
]),
],
'label' => 'New password :',
],
'second_options' => [
'attr' => [
'class' => 'form-control',
],
'label' => 'Repeat it :',
],
'invalid_message' => 'The password fields must match.',
// Instead of being set onto the object directly,
// this is read and encoded in the controller
'mapped' => false,
])
;
}
分析解答
https://symfony.com/doc/current/reference/forms/types/repeated.html#rendering
{# .first and .second may vary in your use - see the note below #}
{{ form_row(form.password.first) }}
{{ form_row(form.password.second) }}