diff --git a/forms.rst b/forms.rst index 38006169cdb..63c45aded50 100644 --- a/forms.rst +++ b/forms.rst @@ -947,6 +947,64 @@ These "unmapped fields" can be set and accessed in a controller with:: Additionally, if there are any fields on the form that aren't included in the submitted data, those fields will be explicitly set to ``null``. +Extra fields +~~~~~~~~~~~~ + +All form fields are considered properties of the object but you can inject +fields directly into your view without specifying them in the form definition. +They can be retrieved via the :method:`FormInterface::getExtraData() ` +method. + +This is a creation user form:: + + // ... + use App\User; + use Symfony\Component\Form\Extension\Core\Type\CheckboxType; + use Symfony\Component\Form\Extension\Core\Type\SubmitType; + use Symfony\Component\Form\FormBuilderInterface; + + class UserCreateType extends AbstractType + { + public function buildForm(FormBuilderInterface $builder, array $options): void + { + $builder + ->add('username', TextType::class) + ->add('email', EmailType::class) + ; + } + + public function configureOptions(OptionsResolver $resolver) + { + $resolver->setDefaults([ + 'data_class' => User::class, + ]); + } + } + +The extra fields can be injected like this: + +.. code-block:: html+twig + + {# templates/user/create.html.twig #} + {{ form_start(form) }} + {{ form_row(form.username) }} + {{ form_row(form.email) }} + + {# Hidden field to send additional referral code #} + + + + {{ form_end(form) }} + +Here, the referral code is an extra field injected at view level. + +You can get the referral code via ``getExtraData``:: + + $extraData = $form->getExtraData(); + $referralCode = $extraData['referralCode'] ?? null; + +> Don't forget to set :ref:`allow_extra_fields ` option to ``true`` on your form + Learn more ---------- pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy