Symfony的学说+ +注释验证表单无法工作

问题描述:

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

I'm using doctrine annotations in my entities definition to define each variable behavior, i.e.:

@ORM\Column(type="string", length=50, nullable=false)

如果我提交表单离开该字段为空,它通过验证,但(Corse车队的)我收到一个错误有关INSERT语句的,因为他不能插入NULL值。
这怎么可能呢?

If I submit the form leaving the field empty, it pass the validation, but (of corse) I receive an error about the INSERT statement, because he cannot insert NULL value. How this could be possible?

这是因为交响乐不会根据教义注解自动验证你的表单输入。

This is because Symphony does not automatically validate your form input based on Doctrine annotations.

Symfony2中附带了一个验证组件,使这个任务简单,透明,虽然。该组件是基于 JSR303 Bean验证规范

Symfony2 ships with a Validator component that makes this task easy and transparent though. This component is based on the JSR303 Bean Validation specification.

http://symfony.com/doc/current/阅读上的实现本书/ validation.html

下面是可能有助于您验证注释的例子:

Here is an example of Validator annotation that might assist you:

// 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;
}