Symfony2 - How to validate an email address in a controller

后端 未结 6 1472
走了就别回头了
走了就别回头了 2021-02-01 18:38

There is an email validator in symfony that can be used in a form: http://symfony.com/doc/current/reference/constraints/Email.html

My question is: How can I use this val

相关标签:
6条回答
  • 2021-02-01 19:01

    Another way - you may use egulias/EmailValidator bundle.

    composer require egulias/email-validator
    

    It can be used without container

        use Egulias\EmailValidator\EmailValidator;
        use Egulias\EmailValidator\Validation\RFCValidation;
        
        $validator = new EmailValidator();
        $validator->isValid("example@example.com", new RFCValidation());
    

    Also bundle can validate DNS with DNSCheckValidation

    0 讨论(0)
  • 2021-02-01 19:08

    I wrote a post about validating email address(es) (one or many) outside of forms

    http://konradpodgorski.com/blog/2013/10/29/how-to-validate-emails-outside-of-form-with-symfony-validator-component/

    It also covers a common bug where you validate against Email Constraint and forget about NotBlank

    /**
     * Validates a single email address (or an array of email addresses)
     *
     * @param array|string $emails
     *
     * @return array
     */
    public function validateEmails($emails){
    
        $errors = array();
        $emails = is_array($emails) ? $emails : array($emails);
    
        $validator = $this->container->get('validator');
    
        $constraints = array(
            new \Symfony\Component\Validator\Constraints\Email(),
            new \Symfony\Component\Validator\Constraints\NotBlank()
        );
    
        foreach ($emails as $email) {
    
            $error = $validator->validateValue($email, $constraints);
    
            if (count($error) > 0) {
                $errors[] = $error;
            }
        }
    
        return $errors;
    }
    

    I hope this helps

    0 讨论(0)
  • 2021-02-01 19:18

    My solution for symfony 3 was the following:

    use Symfony\Component\Validator\Constraints\Email as EmailConstraint;
    
    $email = 'someinvalidmail@invalid.asdf';
    // ... in the action then call
    $emailConstraint = new EmailConstraint();
    
    $errors = $this->get('validator')->validate(
        $email,
        $emailConstraint
    );
    
    $mailInvalid = count($errors) > 0;
    
    0 讨论(0)
  • 2021-02-01 19:20

    If you're creating the form in the controller itself and want to validate email in the action, then the code will look like this.

    // add this above your class
    use Symfony\Component\Validator\Constraints\Email;
    
    public function saveAction(Request $request) 
    {
        $form = $this->createFormBuilder()
            ->add('email', 'email')
            ->add('siteUrl', 'url')
            ->getForm();
    
        if ('POST' == $request->getMethod()) {
            $form->bindRequest($request);
    
            // the data is an *array* containing email and siteUrl
            $data = $form->getData();
    
            // do something with the data
            $email = $data['email'];
    
            $emailConstraint = new Email();
            $emailConstraint->message = 'Invalid email address';
    
            $errorList = $this->get('validator')->validateValue($email, $emailConstraint);
            if (count($errorList) == 0) {
                $data = array('success' => true);
            } else {
                $data = array('success' => false, 'error' => $errorList[0]->getMessage());
            }
       }
    
       return $this->render('AcmeDemoBundle:Default:update.html.twig', array(
           'form' => $form->createView()
       ));
    }
    

    I'm also new and learning it, any suggestions will be appreciated...

    0 讨论(0)
  • 2021-02-01 19:20

    Why does no one mention that you can validate it with in FormBuilder instance using 'constraints' key ??? First of all, read documentation Using a Form without a Class

    'constraints' =>[
        new Assert\Email([
            'message'=>'This is not the corect email format'
        ]),
        new Assert\NotBlank([
            'message' => 'This field can not be blank'
        ])
    ],
    

    Works fine with symfony 3.1

    Example:

    namespace SomeBundle\Controller;
    
    use Symfony\Component\HttpFoundation\Request;
    use Symfony\Component\Form\Extension\Core\Type;
    use Symfony\Component\Validator\Constraints as Assert;
    
    class DefaultController extends Controller
    {
    
        /**
         * @Route("kontakt", name="_kontakt")
         */
        public function userKontaktAction(Request $request) // access for all
        {
    
            $default = array('message' => 'Default input value');
            $form = $this->createFormBuilder($default)
            ->add('name', Type\TextType::class,[
                'label' => 'Nazwa firmy',
            ])
            ->add('email', Type\EmailType::class,[
                'label' => 'Email',
                'constraints' =>[
                    new Assert\Email([
                        'message'=>'This is not the corect email format'
                    ]),
                    new Assert\NotBlank([
                        'message' => 'This field can not be blank'
                    ])
                ],
            ])
            ->add('phone', Type\TextType::class,[
                'label' => 'Telefon',
            ])
            ->add('message', Type\TextareaType::class,[
                'label' => 'Wiadomość',
                'attr' => [
                    'placeholder' => 'Napisz do nas ... '
                ],
            ])
            ->add('send', Type\SubmitType::class,[
                'label' => 'Wyślij',
            ])
            ->getForm();
    
            $form->handleRequest($request);
    
            if ($form->isValid()) {
                // data is an array with "name", "email", and "message" keys
                $data = $form->getData();
                // send email
                // redirect to prevent resubmision
                var_dump($data);
            }
            
            return $this->render('SomeBundle:Default:userKontakt.html.twig', [
                'form' => $form->createView()
            ]);
        }
    
    }
    

    Result:

    See the documentaion about available validation types. http://api.symfony.com/3.1/Symfony/Component/Validator/Constraints.html

    If you want to check what are the available keys other than message, go to documentation at:

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

    or navigate to:

    YourProject\vendor\symfony\symfony\src\Symfony\Component\Validator\Constraints\Email.php

    from there, you will be able to see what else is available.

    public $message = 'This value is not a valid email address.';
    
    public $checkMX = false;
    
    public $checkHost = false;
    
    public $strict; "
    

    Also note that I created and validated form inside the controller which is not a best practice and should only be used for forms, which you will never reuse anywhere else in your application.

    Best practice is to create forms in a separated directory under YourBundle/Form. Move all the code to your new ContactType.php class. (don't forget to import FormBuilder class there as it will not extend your controller and will not have access to this class through '$this')

    [inside ContactType class:]

    namespace AdminBundle\Form;
    
    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilderInterface;
    use Symfony\Component\Form\Extension\Core\Type;
    use Symfony\Component\Validator\Constraints as Assert;
    

    [inside your controller:]

    use YourBundle/Form/ContactType;
    // use ...
    
    //...
    $presetData = []; //... preset form data here if you want to
    $this->createForm('AdminBundle\Form\FormContactType', $presetData) // instead of 'createFormBuilder'
    ->getForm();
    // render view and pass it to twig templet...
    // or send the email/save data to database and redirect the form
    
    0 讨论(0)
  • 2021-02-01 19:21

    By using validateValue method of the Validator service

    use Symfony\Component\Validator\Constraints\Email as EmailConstraint;
    // ...
    
    public function customAction()
    {
        $email = 'value_to_validate';
        // ...
    
        $emailConstraint = new EmailConstraint();
        $emailConstraint->message = 'Your customized error message';
    
        $errors = $this->get('validator')->validateValue(
            $email,
            $emailConstraint 
        );
    
        // $errors is then empty if your email address is valid
        // it contains validation error message in case your email address is not valid
        // ...
    }
    // ...
    
    0 讨论(0)
提交回复
热议问题