简体   繁体   English

symfony2验证规则被忽略

[英]symfony2 validation rule ignored

I'm using the fos_user bundle, and I have two places ( = controllers) in which I create a new user in my site. 我使用的是fos_user捆绑包,在站点中有两个地方(=控制器)可用来创建新用户。

I created a validation rule for unique "email" field in my validation.yml file. 我在validation.yml文件中为唯一的“电子邮件”字段创建了验证规则。

in one of the controllers the validation rule is used, but in the other it is not. 在一个控制器中,使用验证规则,但在另一个中则不使用。 Each controller uses a slightly different form type class. 每个控制器使用略有不同的表单类型类。

I tried using both form type classes in the controller that uses the validation and it worked there (so I'm assuming there is nothing wrong with the form types). 我尝试在使用验证的控制器中同时使用两个表单类型类,并且在那里工作了(所以我假设表单类型没有问题)。

code: 码:

validation.yml entry: 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: ~

"good controller": “好的控制者”:

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(),
    ));

"bad controller": “错误的控制器”:

/**
 * 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;
}

Thanks 谢谢

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM