Skip to content

[Form] Minor (language) improvements #17336

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

Merged
merged 1 commit into from
Oct 10, 2022
Merged
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
20 changes: 9 additions & 11 deletions forms.rst
Original file line number Diff line number Diff line change
Expand Up @@ -840,13 +840,13 @@ Form Type Guessing
~~~~~~~~~~~~~~~~~~

If the object handled by the form includes validation constraints, Symfony can
introspect that metadata to guess the type of your field and set it up for you.
In the above example, Symfony can guess from the validation rules that both the
introspect that metadata to guess the type of your field.
In the above example, Symfony can guess from the validation rules that the
``task`` field is a normal ``TextType`` field and the ``dueDate`` field is a
``DateType`` field.

When building the form, omit the second argument to the ``add()`` method, or
pass ``null`` to it, to enable Symfony's "guessing mechanism"::
To enable Symfony's "guessing mechanism", omit the second argument to the ``add()`` method, or
pass ``null`` to it::

// src/Form/Type/TaskType.php
namespace App\Form\Type;
Expand Down Expand Up @@ -881,23 +881,21 @@ pass ``null`` to it, to enable Symfony's "guessing mechanism"::
Form Type Options Guessing
..........................

When the guessing mechanism is enabled for some field (i.e. you omit or pass
``null`` as the second argument to ``add()``), in addition to its form type,
the following options can be guessed too:
When the guessing mechanism is enabled for some field, in addition to its form type,
the following options will be guessed too:

``required``
The ``required`` option can be guessed based on the validation rules (i.e. is
The ``required`` option is guessed based on the validation rules (i.e. is
the field ``NotBlank`` or ``NotNull``) or the Doctrine metadata (i.e. is the
field ``nullable``). This is very useful, as your client-side validation will
automatically match your validation rules.

``maxlength``
If the field is some sort of text field, then the ``maxlength`` option attribute
can be guessed from the validation constraints (if ``Length`` or ``Range`` is used)
is guessed from the validation constraints (if ``Length`` or ``Range`` is used)
or from the :doc:`Doctrine </doctrine>` metadata (via the field's length).

If you'd like to change one of the guessed values, override it by passing the
option in the options field array::
If you'd like to change one of the guessed values, override it in the options field array::

->add('task', null, ['attr' => ['maxlength' => 4]])

Expand Down