Symfony + Doctrine + Annotation validation not work in forms

◇◆丶佛笑我妖孽 提交于 2020-01-06 18:12:47

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!