Symfony的学说+ +注释验证表单无法工作 [英] Symfony + Doctrine + Annotation validation not work in forms

查看:206
本文介绍了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;
}

这篇关于Symfony的学说+ +注释验证表单无法工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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