php - 注册表单不会触发验证约束

标签 php symfony doctrine-orm symfony-2.1 symfony-forms

我有一个 Doctrine 用户实体,我正在尝试为注册表单添加表单验证器,但它们在任何情况下都不会触发注册表单。

我的用户实体:

namespace JMSHockey\AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
 * My\AppBundle\Entity\User
 *
 * @ORM\Table(name="user")
 * @ORM\Entity(repositoryClass="My\AppBundle\Entity\UserRepository")
 * @UniqueEntity(fields={"email"}, message="Sorry, this email address is already in use.", groups={"registration"})
 * @UniqueEntity(fields={"username"}, message="Sorry, this username is already taken.", groups={"registration"})
 */
class User implements AdvancedUserInterface,\Serializable
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var string $email
     *
     * @ORM\Column(name="email", type="string", length=75, nullable=false)
     * @Assert\Email()
     * @Assert\NotBlank()
     */
    private $email;

    /**
     * @var string $username
     *
     * @ORM\Column(name="username", type="string", length=75, nullable=false)
     * @Assert\NotBlank()
     */
    private $username;

    ...
    }

这是我的 UserType 表单:

namespace My\AppBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

class UserType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('email','email');
        $builder->add('username','text');
        ...
    }

    public function getDefaultOptions(array $options)
    {
        return array(
            'data_class' => 'My\AppBundle\Entity\User',
            'validation_groups' => array('registration'),
        );
    }

    public function getName()
    {
        return 'user';
    }
}

最后是注册表:

namespace My\AppBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

class RegistrationType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('user', new UserType());
    }

    public function getName()
    {
        return 'registration';
    }
}

看来我肯定在这里遗漏了一些明显的东西,但是在 Symfony 手册和我在网上找到的资源之间,我无法确定它是什么。

最佳答案

我遇到了同样的问题。

我通过在 RegistrationType 类的 setDefaultOptions 方法中添加 cascade_validation 为 true 解决了这个问题,并使用了 OptionsResolverInterface 来自 OptionsResolver Symfony 组件的类:

namespace My\AppBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface

class RegistrationType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('user', new UserType());
    }

    public function getName()
    {
        return 'registration';
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
         $resolver->setDefaults(array(
             'cascade_validation' => true
         ));
    }
}

关于php - 注册表单不会触发验证约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12136001/

相关文章:

php - Symfony 服务注入(inject)语法

php - Assets CSS 背景图像未加载但具有相同的文件路径

php - Symfony 在远程服务器上部署项目 ContextErrorException

php - Insert 只存储第一个数字,其余的被忽略

php - 使用 number_format 将发布的值转换为十进制

php - 填充编辑表单上的复选框

php - ZF2 Doctrine 与 objectSelect 获得多对多关系

doctrine-orm - 标识符中的Doctrine 2 ORM DateTime字段

php - 如何在 Symfony2 Doctrine2 中获取不同的 DBAL 连接

php - mysql_fetch_assoc 与迭代 mysql_fetch_row 和 mysql_fetch_column