【问题标题】:How can I separate RepeatedType widget in the view?如何在视图中分离 RepeatedType 小部件?
【发布时间】:2020-06-03 19:45:31
【问题描述】:

我正在使用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,
            ])
        ;
    }

【问题讨论】:

    标签: php symfony symfony-forms


    【解决方案1】:

    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) }}
    

    【讨论】:

      猜你喜欢
      • 2022-10-03
      • 2023-03-20
      • 2018-10-30
      • 1970-01-01
      • 2012-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-08
      相关资源
      最近更新 更多