问题
I'm using doctrine annotations in my entities definition to define each variable behavior, i.e.:
@ORM\Column(type="string", length=50, nullable=false)
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?
回答1:
This is because Symfony does not automatically validate your form input based on Doctrine annotations.
Symfony2 ships with a Validator component that makes this task easy and transparent though. This component is based on the JSR303 Bean Validation specification.
Read up on the implementation at http://symfony.com/doc/current/book/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;
}
来源:https://stackoverflow.com/questions/15814105/symfony-doctrine-annotation-validation-not-work-in-forms