检查Symfony 3.2中的电子邮件是否有效或是否已存在

时间:2016-12-07 10:29:31

标签: php symfony

请尊重我,我是symfony的新手,如果有效或已经存在,我想验证表格。用户可以提交表单并将数据保存在数据库中,问题是用户可以同时提交重复的电子邮件。我该怎么做。

namespace SwipeBundle\Controller\Backend;

use SwipeBundle\Entity\Company;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\MoneyType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\EmailValidator;

   public function addAction(Request $request)
    {   
        $company = new Company;

        $form = $this->createFormBuilder($company)
            ->add('name', TextType::class)
            ->add('prefund_wallet', TextType::class)
            ->add('telephone_no', TextType::class)
            ->add('mobile_no', TextType::class)
            ->add('address', TextareaType::class )
            ->add('email_address', EmailType::class)
            ->add('website', TextType::class)
            ->add('Submit', SubmitType::class)
            ->getForm();


        $form->handleRequest($request);

        if($form->isSubmitted() && $form->isValid())
        {
            $name           = $form['name']->getData();
            $prefund_wallet = $form['prefund_wallet']->getData();
            $telephone_no   = $form['telephone_no']->getData();
            $mobile_no      = $form['mobile_no']->getData();
            $address        = $form['address']->getData();
            $email_address  = $form['email_address']->getData();
            $website        = $form['website']->getData();
            $ip             = $request->getClientIp();

            $now = new\DateTime('now');

            // Check if email is valid
            $emailValidator = new EmailValidator();

            if(!$emailValidator->isValid($email_address, new Email())) 
            { 
              $form['email_address']->addError(new FormError('Invalid email')); 
            }           

            $company->setName($name);
            $company->setPrefundWallet($prefund_wallet);
            $company->setTelephoneNo($telephone_no);
            $company->setMobileNo($mobile_no);
            $company->setAddress($address);
            $company->setEmailAddress($email_address);
            $company->setRegistered($now);
            $company->setIp($ip);
            $company->setWebsite($website);

            $em = $this->getDoctrine()
                ->getManager();
            $em->persist($company);
            $em->flush();

            $this->addFlash(
                'notice', 'Company has been added.'
            );

            die("Redirected");
        }

        return $this->render('Backend/Company/create.html.twig', array(
            'form' => $form->createView()
        ));
    }

2 个答案:

答案 0 :(得分:2)

嗯,有些人没有读过#34;对symfony" ...

的新词

首先要做的是在您的实体中将您的电子邮件设置为唯一:

/**
* @var string
*
* @ORM\Column(name="email_address", type="string", length=255, unique=true)
*/
private $emailAddress;

不要忘记执行doctrine:schema:update --force

下一步将是您已经完成的操作,在FormType中设置类型 顺便说一下,你不需要在任何地方添加TextType::class ......

->add('email_address', EmailType::class)

要检查电子邮件是否来自某些匿名yopmail或类似的东西,您需要创建一个检查电子邮件域的服务。 您也可以在FormType中执行此操作,但是您需要为每个FormType执行此操作,因此服务会更好。您可以在此处轻松找到有关如何创建服务的信息。如果你真的需要帮助,评论,我会写下一个例子。

您可以在任何地方找到这些匿名电子邮件服务的列表,谷歌是您的朋友。

答案 1 :(得分:1)

您可以直接在实体级别检查电子邮件验证。

查看http://symfony.com/doc/current/reference/constraints/Email.html

要检查是否是唯一用途

https://symfony.com/doc/current/reference/constraints/UniqueEntity.html

如果你做对了 - 实体中的代码只有2行(2个断言)