- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 5.3k
[Form] String is the preferred value type for TextType #15404
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
base: 6.1
Are you sure you want to change the base?
[Form] String is the preferred value type for TextType #15404
Conversation
508816b    to
    cb28cef      
    Compare
  
    | @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 | 
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a copy/paste of empty_data documentation
https://symfony.com/doc/current/reference/forms/types/text.html#empty-data
https://github.com/symfony/symfony-docs/blob/6.2/reference/forms/types/options/empty_data_description.rst.inc
There was a problem hiding this comment.
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).
There was a problem hiding this 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 | 
There was a problem hiding this comment.
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).
| 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 :) | 
cb28cef    to
    b6f42ee      
    Compare
  
    | Thank you @peter-gribanov for your contribution! We're closing this PR as the related code PR symfony/symfony#41516 has been closed due to lack of activity. If this documentation change is still needed in the future, please feel free to open a new PR. | 
| Code PR was reopened | 
Reason symfony/symfony#41514 and symfony/symfony#41516
The
text_empty_data_description.rst.incfile is a copy ofempty_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.