Symfony + Doctrine + Annotation 验证在表单中不起作用 [英] Symfony + Doctrine + Annotation validation not work in forms

查看:30
本文介绍了Symfony + Doctrine + Annotation 验证在表单中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

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

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

Symfony2 附带了一个 Validator 组件,这使得这项任务变得简单而透明.此组件基于 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 阅读实施/book/validation.html

以下是可能对您有所帮助的 Validator 注释示例:

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

这篇关于Symfony + Doctrine + Annotation 验证在表单中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆