我有一个带有文字字段的表单。此字段映射到我的数据库中不可为空的字段(是遗留数据库,我无法更改此字段)
问题是Symfony2总是将空文本字段设置为NULL
,这会导致插入失败。
有没有办法告诉Symfony2不要将空文本字段设置为NULL
?
答案 0 :(得分:4)
首先在实体
中默认设置空白值/**
* @ORM\Column(name="example", type="string", length=255, nullable=false)
*/
private $example = '';
至于你的问题,不幸的是you are describing a known issue/bug with Symfony,所以你必须覆盖设置为setter函数的值:
public function setExample($example = null) {
if (empty($example)) {
$example = '';
}
$this->example = $example;
}
答案 1 :(得分:0)
以下代码适合我。我调整了表单定义。
$builder->add('comment', TextareaType::class, [
'empty_data' => '',
'required' => false,
]);