Given this simple form.
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('votes', 'text', array('empty_data' => 0))
;
}
If I leave the field votes
empty when I submit the form $form['votes']->getData()
gives me 0
If I change the field type to number
or integer
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('votes', 'number', array('empty_data' => 0))
;
}
$form['votes']->getData()
gives me null
Is this the correct behavior ? Is this related to #5906 ?
The empty_data
option expects the given data to be in the format of the type's view data. In case of the NumberType
, this must be a string as this is what will be received during submission of the form. Therefore, the observed behaviour is expected and you must use the string '0'
to achieve what you are trying to do. Thus, I am closing here as this is no bug. Thank you for understanding.
Most helpful comment
The
empty_data
option expects the given data to be in the format of the type's view data. In case of theNumberType
, this must be a string as this is what will be received during submission of the form. Therefore, the observed behaviour is expected and you must use the string'0'
to achieve what you are trying to do. Thus, I am closing here as this is no bug. Thank you for understanding.