V
V
vladislav9972020-09-22 16:37:39
symfony
vladislav997, 2020-09-22 16:37:39

How to disassemble form_widget for parts?

Tell me how to customize form_widget? how to make normal inputs from form_widget so that you can work with each one separately?

controller:

public function register(Request $request, UserPasswordEncoderInterface $passwordEncoder, GuardAuthenticatorHandler $guardHandler, LoginFormAuthenticator $formAuthenticator)
    {
        $form = $this->createForm(UserRegistrationFormType::class);
        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {
            /** @var User $user */
            $user = $form->getData();

            $user->setPassword($passwordEncoder->encodePassword($user, $user->getPassword()));

            $em = $this->getDoctrine()->getManager();

            $em->persist($user);
            $em->flush();
        }

        return $this->render('security/register.html.twig', [
            'registrationForm' => $form->createView(),
        ]);


twig:
{{ form_start(registrationForm) }}
                {{ form_widget(registrationForm, {'attr': {'class': 'uk-margin-large'}}) }}
                <button class="btn btn-lg btn-primary btn-block" type="submit">
                Register
                </button>
{{ form_end(registrationForm) }}


How to bring it all back to normal:
<form action="..">
<input ...
<button ...
</form>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Daria Motorina, 2020-09-22
@vladislav997

how to customize forms
The widget consists of a form_row() where each row is responsible for one input. To override the rendering of one row, you need to find the alias of this piece of the template in the vendor of the form component and create a separate template just for this input (or for the widget). Symfonycasts has an explainer video

V
vladislav997, 2020-09-22
@vladislav997

{{ form_start(registrationForm) }}

                <div class="uk-margin">
                    {{ form_label(registrationForm.email, 'Эл. почта', {'label_attr': {'class': 'uk-form-label'}}) }}
                    {{ form_widget(registrationForm.email, {'attr': {'class': 'uk-input'}}) }}
                </div>

                <div class="uk-margin">
                    {{ form_label(registrationForm.password, 'Пароль', {'label_attr': {'class': 'uk-form-label'}}) }}
                    {{ form_widget(registrationForm.password, {'attr': {'class': 'uk-input'}}) }}
                </div>

                <div class="uk-margin" style="display:flex;justify-content:center">
                    <button class="uk-button uk-button-primary" type="submit">
                    Зарегистрироваться
                    </button>
                </div>
                {{ form_end(registrationForm) }}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question