Symfony: `interger` and `number` do not return correct `empty_data`

Created on 17 Nov 2014  路  1Comment  路  Source: symfony/symfony

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 ?

Bug Form Needs Review

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 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.

>All comments

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.

Was this page helpful?
0 / 5 - 0 ratings