验证约束不会触发注册表



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

我的用户实体:

namespace JMSHockeyAppBundleEntity;
use DoctrineORMMapping as ORM;
use SymfonyComponentSecurityCoreUserAdvancedUserInterface;
use SymfonyComponentValidatorConstraints as Assert;
use SymfonyBridgeDoctrineValidatorConstraintsUniqueEntity;
/**
 * MyAppBundleEntityUser
 *
 * @ORMTable(name="user")
 * @ORMEntity(repositoryClass="MyAppBundleEntityUserRepository")
 * @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
     *
     * @ORMColumn(name="id", type="integer", nullable=false)
     * @ORMId
     * @ORMGeneratedValue(strategy="IDENTITY")
     */
    private $id;
    /**
     * @var string $email
     *
     * @ORMColumn(name="email", type="string", length=75, nullable=false)
     * @AssertEmail()
     * @AssertNotBlank()
     */
    private $email;
    /**
     * @var string $username
     *
     * @ORMColumn(name="username", type="string", length=75, nullable=false)
     * @AssertNotBlank()
     */
    private $username;
    ...
    }

这是我的UserType表单:

namespace MyAppBundleFormType;
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilderInterface;
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' => 'MyAppBundleEntityUser',
            'validation_groups' => array('registration'),
        );
    }
    public function getName()
    {
        return 'user';
    }
}

最后,注册表格:

namespace MyAppBundleFormType;
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilderInterface;
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来解决此问题,并使用OptionsResolver Symfony组件中的OptionsResolverInterface类:

namespace MyAppBundleFormType;
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentOptionsResolverOptionsResolverInterface
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
         ));
    }
}

您在哪里找到这个groups选项?

您可以将其留空,这样它将被触发用于此实体。如果您想指定一个实体,看起来应该使用em选项。

http://symfony.com/doc/master/reference/constraints/UniqueEntity.html#em

相关内容

  • 没有找到相关文章

最新更新