@@ -947,6 +947,59 @@ These "unmapped fields" can be set and accessed in a controller with::
947947Additionally, if there are any fields on the form that aren't included in
948948the submitted data, those fields will be explicitly set to ``null ``.
949949
950+ Extra fields
951+ ~~~~~~~~~~~~~~~
952+
953+ All form fields are considered properties of the object but you can inject fields
954+ directly into your view without specifying them in the form definition.
955+ They can be retrieved via the ``getExtraData `` :class: `Symfony\\ Component\\ Form\\ FormTypeInterface `.
956+
957+ This is a creation user form::
958+
959+ // ...
960+ use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
961+ use Symfony\Component\Form\Extension\Core\Type\SubmitType;
962+ use Symfony\Component\Form\FormBuilderInterface;
963+ use App\User;
964+
965+ class UserCreateType extends AbstractType
966+ {
967+ public function buildForm(FormBuilderInterface $builder, array $options): void
968+ {
969+ $builder
970+ ->add('username', TextType::class)
971+ ->add('email', EmailType::class)
972+ ;
973+ }
974+
975+ public function configureOptions(OptionsResolver $resolver)
976+ {
977+ $resolver->setDefaults([
978+ 'data_class' => User::class,
979+ ]);
980+ }
981+ }
982+
983+ The extra fields can be injected like this::
984+
985+ {# templates/user/create.html.twig #}
986+ {{ form_start(form) }}
987+ {{ form_row(form.username) }}
988+ {{ form_row(form.email) }}
989+
990+ {# Hidden field to send additional sponsorship code #}
991+ <input type="hidden" name="user_create[referralCode]" value="{{ referralCode }}" />
992+
993+ <button type="submit">Submit</button>
994+ {{ form_end(form) }}
995+
996+ Here, the sponsorship code is an extra field injected at view level.
997+
998+ You can get the referraCode via ``getExtraData ``::
999+
1000+ $extraData = $form->getExtraData();
1001+ $referraCode = $extraData['referralCode'] ?? null;
1002+
9501003Learn more
9511004----------
9521005
0 commit comments