2014-10-17 61 views
0

晚上好,Symfony的形模型对@unique新电子邮件字段

我添加了一个形式改变用户的电子邮件:

ChangeEmailFormType:

$builder->add('current_email', 'email', array(
     'label' => 'profile.changePassOrEmail.emailLbl', 
     'translation_domain' => 'Startup', 
     'mapped' => false, 
     'attr' => array(
      'readonly' => true, 
      'value' => $this->user->getEmail() 
     ) 
    )) 
    ->add('new_email', 'repeated', array(
     'type' => 'email', 
     'options' => array('translation_domain' => 'Startup'), 
     'first_options' => array('label' => 'profile.changePassOrEmail.newEmail'), 
     'second_options' => array('label' => 'profile.changePassOrEmail.newEmail2'), 
     'invalid_message' => 'profile.changeEmail.mismatch', 
    )) 

对于 'NEW_EMAIL'我生成一个表单模型类如下:

class ChangeEmail 
{ 
/** 
* @var string 
* @Assert\NotBlank() 
* @Assert\Email() 
*/ 
public $new_email; 
} 

我的控制器是以下创建表格和处理它:

*/ 
public function changeEmailAction(Request $request) 
{ 
    $user = $this->container->get('security.context')->getToken()->getUser(); 
    if (!is_object($user) || !$user instanceof UserInterface) { 
     throw new AccessDeniedException('This user does not have access to this section.'); 
    } 


    $form_email = $this->createForm(new ChangeEmailFormType($user)); 
    $form_email->setData(new ChangeEmail()); 

    $form_email->handleRequest($request); 

    if($form_email->isValid()) 
    { 
    }else{ 
    } 

    return $this->render(
     'UserBundle:ChangeEmail:changeEmail.html.twig', 
     array(
      'form_email' => $form_email->createView() 
     ) 
    ); 
} 

这一切工作正常,但我有我的“ChangeEmail”类的问题。 我不知道如何在$ new_email字段中添加“@Unique”注释。 我该如何检查数据库,甚至创建一个自定义注释来检查电子邮件是否已经存在于我的usertable上?在注册表单中,FosUserBundle处理该注册表单。

谢谢。

+0

在这里 - http://symfony.com/doc/current/reference/constraints/UniqueEntity.html – dmnptr 2014-10-17 18:26:09

+0

“验证一个特定的字段(或字段)的学说实体(是)唯一的。” 正如你可以看到,这不是一个学说实体。它是处理提交的表单模型类。就像FosUserBundle正在使用更改密码功能一样。 – 2014-10-17 18:43:50

+0

您发布的链接与我的问题和真正的问题无关,您没有回答:'D – 2014-10-18 10:41:25

回答

0

解决方案:我写了自己的验证器并将其绑定到new_email字段。