Php symfony2验证规则已忽略

Php symfony2验证规则已忽略,php,validation,symfony,Php,Validation,Symfony,我使用的是fos_用户包,我有两个地方(=控制器)可以在我的站点中创建新用户 我在validation.yml文件中为唯一的“email”字段创建了验证规则 在其中一个控制器中使用验证规则,但在另一个控制器中不使用。 每个控制器使用稍微不同的表单类型类 我尝试在使用验证的控制器中使用这两个表单类型类,它在那里工作(因此我假设表单类型没有问题) 代码: validation.yml条目: CRM\CoreBundle\Entity\User: constraints: - Symfony\

我使用的是fos_用户包,我有两个地方(=控制器)可以在我的站点中创建新用户

我在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: ~
“好管制员”:

“坏控制器”:


谢谢

我注意到您正在使用两种表单OwnerRegistrationType和UserType,并希望执行相同的验证,请确保两种表单上都有电子邮件字段

 $builder ->add('email', 'email', array('label' => 'form.email',  'translation_domain' => 'FOSUserBundle'))

是的,有两种不同的表单类型,但它们都具有相同的数据类。而且,正如我在问题中所写的,我尝试在不同的控制器中使用这两种表单类型,并且行为持续存在。
/**
 * 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;
}
 $builder ->add('email', 'email', array('label' => 'form.email',  'translation_domain' => 'FOSUserBundle'))