5

我有一个 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;

    ...
    }

这是我的用户类型表单:

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 手册和我在网上找到的资源之间,我无法确定它是什么。

4

2 回答 2

2

我有同样的问题。

我通过在您的类的方法中添加cascade_validationtrue 来解决它,并使用OptionsResolver Symfony 组件中的类:setDefaultOptionsRegistrationTypeOptionsResolverInterface

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
         ));
    }
}
于 2012-12-16T13:28:48.350 回答
0

你在哪里找到这个组选项?

您可以将其留空,以便为该实体触发。如果你想指定一个实体,看起来你应该使用 em 选项。

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

于 2012-08-27T10:02:04.513 回答