简体   繁体   中英

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.

I created a validation rule for unique "email" field in my validation.yml file.

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:

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'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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