EasyAdminBundle
EasyAdminBundle copied to clipboard
Suggestion: setEmptyData() method in FieldTrait
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.
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
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.
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 :)
Fixed in #5154.