Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
Arkalo2 wants to merge12 commits intosymfony:6.4
base:6.4
Choose a base branch
Loading
fromArkalo2:form/add-extra-data
Open
Changes fromall 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
58 changes: 58 additions & 0 deletionsforms.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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() <Symfony\\Component\\Form\\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 #}
<input type="hidden" name="{{ form.vars.full_name ~ '[referralCode]' }}" value="{{ referralCode }}"/>

<button type="submit">Submit</button>
{{ 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 <allow-extra-fields>` option to ``true`` on your form

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

Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp