Symfony2 - How to validate an email address in a controller

后端 未结 6 1482
走了就别回头了
走了就别回头了 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: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;
    

提交回复
热议问题