2

我有一个这样定义的自定义注册表单类型:

....
public function buildForm(FormBuilderInterface $builder, array $options)
    {
        parent::buildForm($builder, $options);

        $builder
            ->remove('username')
            ->add('firstName')
            ->add('lastName')
            ->add('hei', 'entity', array(
                'class' => 'AcmeAcmeBundle:HigherEducationalInstitution',
                'label' => 'Higher Educational Institution'
            ));

    }
....

自定义控制器的工作方式与 FOSUserbundle 中的控制器几乎相同,并且还会检查有效表单

...
public function registerAsStudentAction(Request $request)
    {
        /** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
        $formFactory = $this->get('acme.user_form_factory');
        /** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */
        $userManager = $this->get('fos_user.user_manager');
        /** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
        $dispatcher = $this->get('event_dispatcher');

        $user = $userManager->createUser();
        $user->setEnabled(true);

        $dispatcher->dispatch(FOSUserEvents::REGISTRATION_INITIALIZE, new UserEvent($user, $request));

        $form = $formFactory->getStudentRegistrationForm();
        $form->setData($user);

        if ('POST' === $request->getMethod()) {
            $form->bind($request);

            if ($form->isValid()) {
                $event = new FormEvent($form, $request);
                $dispatcher->dispatch(FOSUserEvents::REGISTRATION_SUCCESS, $event);

                $user->addRole('ROLE_STUDENT');

                $userManager->updateUser($user);


                if (null === $response = $event->getResponse()) {
                    $url = $this->get('router')->generate('fos_user_registration_confirmed');
                    $response = new RedirectResponse($url);
                }

                $dispatcher->dispatch(FOSUserEvents::REGISTRATION_COMPLETED, new FilterUserResponseEvent($user, $request, $response));

                return $response;
            }
        }

        return $this->render('AcmeUserBundle:Registration:register_student.html.twig', array('form' => $form->createView()));
    }
....

当我尝试使用已在使用的电子邮件地址进行注册时,我收到了一个原则例外,即电子邮件上唯一键的重复条目。

在正常的注册表单中,我收到一个表单错误,显示电子邮件地址已被使用。表单如何通过验证器,在我的表单中使用重复的电子邮件地址,但在原始注册表中却没有?

4

2 回答 2

2

通过向 AcmeBundle/Resources/config 添加额外的 validation.yml 来修复它

Acme\UserBundle\Entity\User:
    constraints:
        - Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity: { fields: email, message: "This email has already been registered"}
        - Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity: emailCanonical
    properties:
        email:
            - Email: ~
        emailCanonical:
            - Email:  ~
        plainPassword:
            - Length:
                min: 7
                minMessage: "Your password must be at least {{ limit }} characters"
于 2013-06-21T14:43:53.247 回答
1

您可以在实体上添加此注释:

@UniqueEntity(fields="email", message="Email already taken")
于 2019-02-20T14:07:10.170 回答