@@ -22,6 +23,18 @@
*/
class EmailValidator extends ConstraintValidator
{
+ /**
+ * isStrict
+ *
+ * @var Boolean
+ */
+ private $isStrict;
+
+ public function __construct($strict = false)
+ {
+ $this->isStrict = $strict;
+ }
+
/**
* {@inheritDoc}
*/
@@ -36,12 +49,23 @@ public function validate($value, Constraint $constraint)
}
$value = (string) $value;
- $valid = filter_var($value, FILTER_VALIDATE_EMAIL);
+ if (null === $constraint->strict) {
+ $constraint->strict = $this->isStrict;
+ }
+
+ if ($constraint->strict && class_exists('\Egulias\EmailValidator\EmailValidator')) {
+ $strictValidator = new StrictEmailValidator();
+ $valid = $strictValidator->isValid($value, false);
+ } elseif ($constraint->strict === true) {
+ throw new \RuntimeException('Strict email validation requires egulias/email-validator');
+ } else {
+ $valid = preg_match('/.+\@.+\..+/', $value);
+ }
if ($valid) {
$host = substr($value, strpos($value, '@') + 1);
-
// Check for host DNS resource records
+
if ($valid && $constraint->checkMX) {
$valid = $this->checkMX($host);
} elseif ($valid && $constraint->checkHost) {
diff --git a/src/Symfony/Component/Validator/Tests/Constraints/EmailValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/EmailValidatorTest.php
index 701ab1f556df..27c142f6154b 100644
--- a/src/Symfony/Component/Validator/Tests/Constraints/EmailValidatorTest.php
+++ b/src/Symfony/Component/Validator/Tests/Constraints/EmailValidatorTest.php
@@ -22,7 +22,7 @@ class EmailValidatorTest extends \PHPUnit_Framework_TestCase
protected function setUp()
{
$this->context = $this->getMock('Symfony\Component\Validator\ExecutionContext', array(), array(), '', false);
- $this->validator = new EmailValidator();
+ $this->validator = new EmailValidator(false);
$this->validator->initialize($this->context);
}
@@ -100,7 +100,14 @@ public function getInvalidEmails()
array('example'),
array('example@'),
array('example@localhost'),
- array('example@example.com@example.com'),
);
}
+
+ public function testStrict()
+ {
+ $this->context->expects($this->never())
+ ->method('addViolation');
+
+ $this->validator->validate('example@localhost', new Email(array('strict' => true)));
+ }
}
diff --git a/src/Symfony/Component/Validator/composer.json b/src/Symfony/Component/Validator/composer.json
index af14eabdb63a..2e97389ca51e 100644
--- a/src/Symfony/Component/Validator/composer.json
+++ b/src/Symfony/Component/Validator/composer.json
@@ -26,7 +26,8 @@
"symfony/yaml": "~2.0",
"symfony/config": "~2.2",
"doctrine/annotations": "~1.0",
- "doctrine/cache": "~1.0"
+ "doctrine/cache": "~1.0",
+ "egulias/email-validator": "~1.0"
},
"suggest": {
"doctrine/annotations": "For using the annotation mapping. You will also need doctrine/cache.",
@@ -34,7 +35,8 @@
"symfony/http-foundation": "",
"symfony/intl": "",
"symfony/yaml": "",
- "symfony/config": ""
+ "symfony/config": "",
+ "egulias/email-validator": "Strict (RFC compliant) email validation"
},
"autoload": {
"psr-0": { "Symfony\\Component\\Validator\\": "" }
pFad - Phonifier reborn
Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies:
Alternative Proxy
pFad Proxy
pFad v3 Proxy
pFad v4 Proxy