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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Form] Add form option to not convert empty strings to null #28518

Closed
zanbaldwin opened this issue Sep 19, 2018 · 5 comments

Comments

Projects
None yet
4 participants
@zanbaldwin
Copy link
Contributor

commented Sep 19, 2018

馃殌

Description

As Symfony is being used to create more and more APIs, add a form option to not convert empty strings to null without having to attach a DataTransformerInterface instance that returns the value unchanged on every text field.

Also, I feel that the current behaviour to convert empty strings to null should be a data transformer itself, rather than hard-coded in Symfony\Component\Form\Form::viewToNorm().

Example

/** @var \Symfony\Component\Form\FormBuilderInterface $formBuilder */
$formBuilder->create(MyFormType::class, new MyModel, [
    'preserve_empty_strings' => true,
]);

@zanbaldwin zanbaldwin changed the title [Form] Add form option to not convert empty strings to null :rocket: [Form] Add form option to not convert empty strings to null Sep 19, 2018

@yceruto

This comment has been minimized.

Copy link
Contributor

commented Sep 19, 2018

It's actually possible, by passing explicitly '' (empty string) to empty_data option for text fields, you'll get an empty string if an empty value is submitted.

Even though the documentation says it's the default value, actually, it's not at the time TextType is being built. Hence, the data transformer is not added and the default one is used, returning null by default.

@xabbuh

This comment has been minimized.

Copy link
Member

commented Sep 23, 2018

@zanbaldwin Can you confirm that configuring your form type as described by @yceruto solves your problem?

@zanbaldwin

This comment has been minimized.

Copy link
Contributor Author

commented Sep 23, 2018

Hi, it seems to work - I was going to test on Monday whether it would also work for other text-based fields like Choice and Date.

@zanbaldwin

This comment has been minimized.

Copy link
Contributor Author

commented Sep 24, 2018

Unfortunately the empty_data option doesn't give any way to differentiate between an empty string and null.

Tested with both TextType and EmailType, from Symfony Skeleton v4.1.2.3 with a form type, controller and route definition, and JSON payload listener. The exact code I used to test can be found at: https://gist.github.com/zanbaldwin/514bcf0e4ebf04e79efac18df5f1df1e

  • With empty_data not set (eg ->add('my_text', TextType::class)):
    • Submitting {"my_text":null} results in $form->getData()['my_text'] being null.
    • Submitting {"my_text":""} results in $form->getData()['my_text'] being null.
  • With empty_data set (eg ->add('my_text', TextType::class, ['empty_data' => ''])):
    • Submitting {"my_text":null} results in $form->getData()['my_text'] being string(0) "".
    • Submitting {"my_text":""} results in $form->getData()['my_text'] being string(0) "".
@yceruto

This comment has been minimized.

Copy link
Contributor

commented Apr 6, 2019

There is no bug here. The behavior is expected and the related document has been updated already.
Also if you want to change the current behavior it's possible through a form type extension with ease.
IMO can be closed.

@fabpot fabpot closed this Apr 6, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can鈥檛 perform that action at this time.