Skip to content

[Form] form add extra data #20853

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: 6.4
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions forms.rst
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,72 @@ 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 ``getExtraData`` :class:`Symfony\\Component\\Form\\FormTypeInterface`.

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 #}
<input type="hidden" name="user_create[referralCode]" value="{{ referralCode }}"/>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isnt it usefull to also add a note on from where this "user_create" value comes from?
as it is a "niche feature" I would assume one knows but just in case ^^

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A note to explain user_create is the name of the form?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes because it will only works of this handwritten name is correct according to symfony way of doing right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added description, what do you thinks ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it, much more precise imho

Copy link
Member

@yceruto yceruto Apr 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or you can use {{ form.vars.full_name ~ '[referralCode]' }} to render the form name dynamically

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or you can use {{ form.vars.full_name ~ '[referralCode]' }} to render the form name dynamically

Ok I added it


<button type="submit">Submit</button>
{{ form_end(form) }}

Here, the referral code is an extra field injected at view level.

The field name is composed of form ``user_create`` and the field name ``referralCode``.
It's automatically generated from the form class name. You can :ref:`override it <changing-the-form-name>`

Or you can use

.. code-block:: twig

{{ form.vars.full_name ~ '[referralCode]' }}

to render the form name dynamically

You can get the referral code via ``getExtraData``::

$extraData = $form->getExtraData();
$referralCode = $extraData['referralCode'] ?? null;

Learn more
----------

Expand Down