我正在使用fos_user包,我有两个地方(=控制器),我在我的网站中创建了一个新用户。
我在validation.yml文件中为唯一的“email”字段创建了验证规则。
在其中一个控制器中使用验证规则,但在另一个控制器中则不然。 每个控制器使用略有不同的表单类型。
我尝试在使用验证的控制器中使用两种表单类型类并在那里工作(所以我假设表单类型没有任何问题)。
代码:
validation.yml条目:
CRM\CoreBundle\Entity\User:
constraints:
- Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity: email
- Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity: username
- Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity: mobile
properties:
email:
- Email: ~
“好控制者”:
public function registerAction(Request $request)
{
$owner = new User();
$form = $this->createForm(new OwnerRegistrationType(), $owner);
$form->add('captcha', 'captcha');
$form->add('submit', 'submit');
$form->handleRequest($request);
if ($form->isValid()) {
$account = new Account();
$account->setFrozen(true);
$owner->setAccount($account);
$owner->addRole('ROLE_ACCOUNT_OWNER');
$owner->setEnabled(true);
$em = $this->getDoctrine()->getManager();
$em->persist($account);
$em->persist($owner);
$em->flush();
$settings = $this->get('crm_core.account_settings');
$settings->setAccountId($account->getId());
$settings->prop(AccountSetting::PHONE, $owner->getMobile());
$settings->prop(AccountSetting::MAIN_MAIL, $owner->getEmail());
$settings->prop(AccountSetting::USER_LIMIT, 1);
$verification = $this->get('crm_phone_verification.model.verification')->createVerification(
$owner->getMobile(),
array(
'action' => 'registration',
'accountId' => $account->getId(),
)
);
return $this->redirectToRoute('verification_phone', array('slug' => $verification->getSlug()));
}
return $this->render('CRMCoreBundle:Registration:registration.html.twig', array(
'form' => $form->createView(),
));
“坏控制器”:
/**
* Creates a new User entity.
*
*/
public function createAction(Request $request)
{
$this->get('event_dispatcher')->dispatch('user.create_attempt');
$entity = new User();
$entity->setAccount($this->getUser()->getAccount());
$form = $this->createCreateForm($entity);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('user_show', array('id' => $entity->getId())));
}
return $this->render(
'CRMCoreBundle:User:new.html.twig',
array(
'entity' => $entity,
'form' => $form->createView(),
)
);
}
/**
* Creates a form to create a User entity.
*
* @param User $entity The entity
*
* @return \Symfony\Component\Form\Form The form
*/
private function createCreateForm(User $entity)
{
$form = $this->createForm(
new UserType(),
$entity,
array(
'action' => $this->generateUrl('user_create'),
'method' => 'POST',
)
);
$form->add(
'plainPassword',
'repeated',
array(
'type' => 'password',
'options' => array('translation_domain' => 'FOSUserBundle'),
'first_options' => array('label' => 'form.new_password'),
'second_options' => array('label' => 'form.new_password_confirmation'),
'invalid_message' => 'fos_user.password.mismatch',
)
);
$form->add('submit', 'submit', array('label' => 'הוספה'));
return $form;
}
由于
答案 0 :(得分:0)
我注意到您使用两种类型的表单OwnerRegistrationType,UserType并期望执行相同的验证,请确保您在两个表单上都有电子邮件字段。
$builder ->add('email', 'email', array('label' => 'form.email', 'translation_domain' => 'FOSUserBundle'))