簡體   English   中英

symfony2驗證規則被忽略

[英]symfony2 validation rule ignored

我使用的是fos_user捆綁包,在站點中有兩個地方(=控制器)可用來創建新用戶。

我在validation.yml文件中為唯一的“電子郵件”字段創建了驗證規則。

在一個控制器中,使用驗證規則,但在另一個中則不使用。 每個控制器使用略有不同的表單類型類。

我嘗試在使用驗證的控制器中同時使用兩個表單類型類,並且在那里工作了(所以我假設表單類型沒有問題)。

碼:

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

謝謝

我注意到您正在使用兩種類型的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