Fix for bad regexp used in PHP <= 5.3.2. Closes #897

This commit is contained in:
Joseph Bielawski 2011-05-14 03:43:46 -07:00
parent dbdb3da6bf
commit d235570653

View File

@ -17,7 +17,6 @@ use Symfony\Component\Validator\Exception\UnexpectedTypeException;
class EmailValidator extends ConstraintValidator
{
public function isValid($value, Constraint $constraint)
{
if (null === $value || '' === $value) {
@ -29,22 +28,27 @@ class EmailValidator extends ConstraintValidator
}
$value = (string) $value;
$valid = filter_var($value, FILTER_VALIDATE_EMAIL);
if (!filter_var($value, FILTER_VALIDATE_EMAIL)) {
$this->setMessage($constraint->message, array('{{ value }}' => $value));
return false;
}
if ($constraint->checkMX) {
if ($valid) {
$host = substr($value, strpos($value, '@') + 1);
if (!$this->checkMX($host)) {
// Likely not a FQDN, bug in PHP FILTER_VALIDATE_EMAIL prior to PHP 5.3.3
if (version_compare(PHP_VERSION, '5.3.3', '<') && strpos($host, '.') === false) {
$valid = false;
}
// Do not check MX records if host is invalid
if ($valid && $constraint->checkMX) {
$valid = $this->checkMX($host));
}
}
if (!$valid) {
$this->setMessage($constraint->message, array('{{ value }}' => $value));
return false;
}
}
return true;
}