Symfony:使用type =' text'使用表单类型'复选框'

时间:2015-08-21 13:44:05

标签: forms symfony

有没有办法使用复选框表单类型和已设置为字符串的字段?

此字段可以包含多个值,可用于将数据作为文本字段或复选框输入。

我在formtype上有一个事件监听器来检查它是否应该是一个复选框或文本字段。

/Entity.php

/**
 * @var string 
 *
 * @ORM\Column(name="value", type="string", length=200, nullable=true)
 */
private $value;

formtype.php

$form->add('value', 'checkbox', array(

))

错误

Unable to transform value for property path "value": Expected a Boolean.

2 个答案:

答案 0 :(得分:6)

Entity.php

public function getValue()
{
    return (boolean)$this->value;
}

答案 1 :(得分:0)

这对我有用:

重写实体中的函数,在返回

中添加显式布尔值
public function getEstado() {
    if ($this->estado===1) {
        return TRUE;
    }
    return FALSE;
}