Symfony2 forms interpret blank strings as nulls

前端 未结 7 2275
温柔的废话
温柔的废话 2021-02-19 09:12

I have a Symfony2 form with a variety of fields, including one optional text field called recap.

This recap field saves perfectly when there\'s

7条回答
  •  难免孤独
    2021-02-19 09:39

    Just came across similar error in Symfony2:

    My entity:

    /**
     * @ORM\Column(type="boolean")
     */
    protected $Valid;
    

    My form:

    $builder->add('Valid', 'hidden');
    

    Now for true values, field is rendered correctly with value="1", but for false values field is rendered with value="", which leads to:

    SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'Valid' cannot be null
    

    While I would expect it to be rendered as value="0".

    Of course setValid() method can be enhanced to set the right value:

    public function setValid($valid)
    {
        $this->Valid = ($valid ? true : false);
    }
    

    Which work arounds the issue, not fixes it.

提交回复
热议问题