EasyAdminBundle icon indicating copy to clipboard operation
EasyAdminBundle copied to clipboard

Suggestion: setEmptyData() method in FieldTrait

Open a-r-m-i-n opened this issue 2 years ago • 3 comments

According to the setRequired method which already exists, it would be nice to have a shortcut for the empty_data form option as well.

Almost always, when I use ->setRequired(false) I need also need to define the empty_data option, to avoid errors like

Expected argument of type "string", "null" given at property path "xyz".

Alternativley or additionally you could extend the setRequired signature by a second parameter, defining the empty_data option. Because required and empty_data are related.

a-r-m-i-n avatar Apr 02 '22 19:04 a-r-m-i-n

Hello @a-r-m-i-n,

You can set up default value at Doctrine entity level or add this to your field configuration:

->setFormTypeOption('empty_data', $value)

or

->setFormTypeOptions(['required' => false, 'empty_data' => $value])

Regards,

Fabien

fabienlem avatar Apr 07 '22 18:04 fabienlem

I know that you can define it that way. I need it very often, so I thought an own setter will reduce field configuration code. Like setRequired do.

a-r-m-i-n avatar Apr 08 '22 08:04 a-r-m-i-n

Setting default on Doctrine level is currently not enough. Without empty_data set to for example an empty string form will provide null to the setter, which might expect string (it happens before validation). Therefore it is often required to set empty_data to '' to avoid type errors.

I for one would benefit from such a shortcut, as ->setFormTypeOption('empty_data', '') is used a lot and suggested helper would make my code cleaner :)

Lustmored avatar May 13 '22 12:05 Lustmored

Fixed in #5154.

javiereguiluz avatar Sep 26 '22 18:09 javiereguiluz