Skip to content
Advertisement

How can I separate RepeatedType widget in the view?

I am working on a ResetPassword system and this is the page where the user writes his new password after he gets an email.

Basically what I want to do is to separate the RepeatedType in the view page!

In the view(.twig) I have :

{{ form_start(resetForm) }}
    {{ form_widget(resetForm.plainPassword) }}
    <button>Reset password</button> 
{{ form_end(resetForm) }}

Instead of

{{ form_widget(resetForm.plainPassword) }}

I want to separate them and make something like :

{{ form_widget(resetForm.plainPassword[1]) }}
{{ form_widget(resetForm.plainPassword[2]) }}

Is that possible? If yes what is the proper syntax to do it?

Here is the code of the 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,
            ])
        ;
    }

Advertisement

Answer

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) }}
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement