Symfony + Doctrine + Annotation验证不适用于表单

Bag*_*yte 0 forms validation annotations symfony doctrine-orm

我在我的实体定义中使用doctrine注释来定义每个变量行为,即:

@ORM\Column(type="string", length=50, nullable=false)
Run Code Online (Sandbox Code Playgroud)

如果我提交表单,将该字段留空,则通过验证,但是(对于corse)我收到有关INSERT语句的错误,因为他无法插入NULL值.这怎么可能?

bub*_*bba 6

这是因为Symphony不会根据Doctrine注释自动验证您的表单输入.

Symfony2附带一个Validator组件,使该任务变得简单透明.该组件基于JSR303 Bean Validation规范.

阅读http://symfony.com/doc/current/book/validation.html上的实施

以下是Validator注释的示例,可以帮助您:

// Acme/TaskBundle/Entity/Example.php
use Symfony\Component\Validator\Constraints as Assert;

class Example
{
    /**
     * @Assert\NotBlank()
     * @Assert\MaxLength(50)
     * @ORM\Column(type="string", length=50, nullable=false)
     */
    public $parameter;
}
Run Code Online (Sandbox Code Playgroud)