没有 Doctrine 实体的验证表单 [英] Validating form without Doctrine Entity

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

问题描述

我有一种情况,我需要验证表单,而实际上没有对象可以存储在任何地方.在这种情况下,我是否仍然会创建一个没有原则的实体并像往常一样验证它以执行我想做的任何事情(如果表单有效或有其他方法)?

I have a situation where I need to validate a form without actually having an object to store anywhere. In this scenario, would I still create an Entity without doctrine and validate it as ususal to perform whatever I want to do if form is valid or is there another way?

例如,以用户的名字向用户发送电子邮件.

Example would be sending an email to a user by his name.

推荐答案

参见 在没有类的情况下使用表单 部分 - 还有一个关于验证的小节.

See the Using a Form without a Class section — there is a subsection on validation, too.

答案是自己设置约束,并将它们附加到各个字段.

The answer is to setup the constraints yourself, and attach them to the individual fields.

use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;

$builder
   ->add('firstName', 'text', array(
       'constraints' => new Length(array('min' => 3)),
   ))
   ->add('lastName', 'text', array(
       'constraints' => array(
           new NotBlank(),
           new Length(array('min' => 3)),
       ),
   ))
;

这篇关于没有 Doctrine 实体的验证表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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