Skip to content
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

[Form] String is the preferred value type for TextType #15404

Open
wants to merge 1 commit into
base: 6.1
Choose a base branch
from

Conversation

peter-gribanov
Copy link

@peter-gribanov peter-gribanov commented Jun 3, 2021

Reason symfony/symfony#41514 and symfony/symfony#41516

The text_empty_data_description.rst.inc file is a copy of empty_data_description.rst.inc:

This option determines what value the field will *return* when the submitted
value is empty (or missing). It does not set an initial value if none is
provided when the form is rendered in a view.

This means it helps you handling form submission with blank fields. For
example, if you want the ``name`` field to be explicitly set to ``John Doe``
when no value is selected, you can do it like this::

    $builder->add('name', null, [
        'required'   => false,
        'empty_data' => 'John Doe',
    ]);

This will still render an empty text box, but upon submission the ``John Doe``
value will be set. Use the ``data`` or ``placeholder`` options to show this
initial value in the rendered form.

If a form is compound, you can set ``empty_data`` as an array, object or
closure. See the :doc:`/form/use_empty_data` article for more details about
these options.

.. note::

    If you want to set the ``empty_data`` option for your entire form class,
    see the :doc:`/form/use_empty_data` article.
-
-.. caution::
-
-    :doc:`Form data transformers </form/data_transformers>` will still be
-    applied to the ``empty_data`` value. This means that an empty string will
-    be cast to ``null``. Use a custom data transformer if you explicitly want
-    to return the empty string.

@javiereguiluz
Copy link
Member

@xabbuh do you have any comments about this proposal? Thanks!

]);

This will still render an empty text box, but upon submission the ``John Doe``
value will be set. Use the ``data`` or ``placeholder`` options to show this
Copy link
Member

@yceruto yceruto Aug 8, 2022

Choose a reason for hiding this comment

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

I wouldn't encourage the use of the inner data option for the default value. Cause that when it's a mapped field the data value set here will replace the bound value that comes from the underlying subject, thus it doesn't behave as a default value. There is already a warning note about it in
https://symfony.com/doc/current/reference/forms/types/text.html#data

We should use the underlying subject to the root form to set default values or a PRE_SET_DATA event listener instead.

Copy link
Member

Choose a reason for hiding this comment

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

and the placeholder option is only for display but it also doesn't actually set an initial value for this field

Copy link
Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

I see, maybe that's something to fix from 4.4 (in another PR).

Copy link
Member

@yceruto yceruto left a comment

Choose a reason for hiding this comment

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

LGTM

]);

This will still render an empty text box, but upon submission the ``John Doe``
value will be set. Use the ``data`` or ``placeholder`` options to show this
Copy link
Member

Choose a reason for hiding this comment

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

I see, maybe that's something to fix from 4.4 (in another PR).

@wouterj wouterj added the Waiting Code Merge Docs for features pending to be merged label Sep 11, 2022
@carsonbot carsonbot added this to the next milestone Sep 11, 2022
@wouterj
Copy link
Member

wouterj commented Sep 11, 2022

As far as I can see, this PR requires the 2 PRs submitted to the code repository to be merged first. I've added the "waiting code merge" label to clarify it.

If this is wrong, please let us know :)

@peter-gribanov peter-gribanov changed the base branch from 5.4 to 6.1 September 27, 2022 11:35
@peter-gribanov peter-gribanov force-pushed the form_text_type_stringable_upgrade branch from cb28cef to b6f42ee Compare September 27, 2022 11:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Form Status: Reviewed Waiting Code Merge Docs for features pending to be merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants