PHP/Symfony2 Form Checkbox field

老子叫甜甜 提交于 2020-01-03 11:01:49

问题


Orm

My\SampleBundle\Entity\Subject:
    type: entity
    id:
        id:
            type: integer
            generator: { strategy: AUTO }
    fields:

        // ...

        motion:
            type: smallint
            unsigned: true

Type

public function buildForm(FormBuilderInterface $builder, array $options)
{
    // ...

    $builder->add('motion', 'checkbox', array(
        'required'  => false
    ));

    // ...
}

Error

Expected argument of type "Boolean", "integer" given


I would like to turn on and off by a check box. The value is distributed by 0 and 1.
It was useless even if it gave the value parameter.

$builder->add('motion', 'checkbox', array(
    'value'     => 1,
    'required'  => false
));

How should I do?


回答1:


In your ORM mapping definition, you have to define motion as a boolean instead of a smallint. And FYI, Symfony interprets TINYINT as boolean and any other integer SQL types as integers.

My\SampleBundle\Entity\Subject:
    type: entity
    id:
        id:
            type: integer
            generator: { strategy: AUTO }
    fields:

        // ...

        motion:
            type: boolean


来源:https://stackoverflow.com/questions/13800832/php-symfony2-form-checkbox-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!