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 validator in my controlelr in order to validate an email address?
This is possible by using the PHP preg_match for usere, but my question is if there is a possibility to use the Symfony already built in email validator.
Thank you in advance.
The best way to "validate" an email addresses is to simply have them type it twice and run a Regex check that gives a WARNING to the user that it doesn't look like a valid email address if it does not match the pattern, and asks the user to double check.
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 // ... } // ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With