Symfony2 FOSUserBundle extending registration form causes duplicate email to validate(Symfony2 FOSUserBundle 扩展注册表导致重复的电子邮件验证)
问题描述
我有一个自定义的注册表单类型定义如下:
I have a custom registration form type defined like this:
....
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 中的控制器几乎相同,并且还会检查有效表单
The custom controller works pretty much the same as the one in the FOSUserbundle and also checks for a valid form
...
public function registerAsStudentAction(Request $request)
{
/** @var $formFactory FOSUserBundleFormFactoryFactoryInterface */
$formFactory = $this->get('acme.user_form_factory');
/** @var $userManager FOSUserBundleModelUserManagerInterface */
$userManager = $this->get('fos_user.user_manager');
/** @var $dispatcher SymfonyComponentEventDispatcherEventDispatcherInterface */
$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()));
}
....
当我尝试使用已在使用的电子邮件地址进行注册时,我收到了一个原则性例外,即电子邮件上唯一键的重复条目.
When I try to register with an email address that's already in use I'm getting a doctrine exception for a duplicate entry for a unique key on the email.
在正常的注册表单中,我收到一个表单错误,显示电子邮件地址已被使用.表单如何通过在我的表单中但在原始注册表中没有重复电子邮件地址的验证器?
In the normal registration form I'm getting a form error displaying that the email address was already used. How can the form pass the validator with a duplicate email address in my form but not in the original registration form?
推荐答案
通过在 AcmeBundle/Resources/config 中添加额外的validation.yml 来修复它
Fixed it by adding extra validation.yml to AcmeBundle/Resources/config
AcmeUserBundleEntityUser:
constraints:
- SymfonyBridgeDoctrineValidatorConstraintsUniqueEntity: { fields: email, message: "This email has already been registered"}
- SymfonyBridgeDoctrineValidatorConstraintsUniqueEntity: emailCanonical
properties:
email:
- Email: ~
emailCanonical:
- Email: ~
plainPassword:
- Length:
min: 7
minMessage: "Your password must be at least {{ limit }} characters"
这篇关于Symfony2 FOSUserBundle 扩展注册表导致重复的电子邮件验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Symfony2 FOSUserBundle 扩展注册表导致重复的电子邮件验证
基础教程推荐
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- PHP 守护进程/worker 环境 2022-01-01
- 使用 PDO 转义列名 2021-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- HTTP 与 FTP 上传 2021-01-01