Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions src/Illuminate/Validation/Rules/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

namespace Illuminate\Validation\Rules;

use Closure;
use Illuminate\Contracts\Validation\DataAwareRule;
use Illuminate\Contracts\Validation\Rule;
use Illuminate\Contracts\Validation\ValidatorAwareRule;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\Conditionable;
use Illuminate\Support\Traits\Macroable;
use InvalidArgumentException;
Expand All @@ -21,6 +23,8 @@ class Email implements Rule, DataAwareRule, ValidatorAwareRule
public bool $nativeValidationWithUnicodeAllowed = false;
public bool $rfcCompliant = false;
public bool $strictRfcCompliant = false;
private array $allowedDomains = [];
private array $disallowedDomains = [];

/**
* The validator performing the validation.
Expand Down Expand Up @@ -162,6 +166,32 @@ public function withNativeValidation(bool $allowUnicode = false)
return $this;
}

/**
* Ensure the domain of the email address matches one of the given patterns.
*
* @param array $domains
* @return $this
*/
public function domainIs($domains)
{
$this->allowedDomains = $domains;

return $this;
}

/**
* Ensure the domain of the email address does not match any of the given patterns.
*
* @param array $domains
* @return $this
*/
public function domainIsNot($domains)
{
$this->disallowedDomains = $domains;

return $this;
}

/**
* Specify additional validation rules that should be merged with the default rules during validation.
*
Expand Down Expand Up @@ -245,6 +275,22 @@ protected function buildValidationRules()
$rules = ['email'];
}

if ($this->allowedDomains) {
$rules[] = function (string $attribute, mixed $value, Closure $fail): void {
if (! $this->domainMatchesPattern($value, $this->allowedDomains)) {
$fail('The :attribute must be a valid email address.');
}
};
}

if ($this->disallowedDomains) {
$rules[] = function (string $attribute, mixed $value, Closure $fail): void {
if ($this->domainMatchesPattern($value, $this->disallowedDomains)) {
$fail('The :attribute must be a valid email address.');
}
};
}

return array_merge(array_filter($rules), $this->customRules);
}

Expand Down Expand Up @@ -283,4 +329,18 @@ public function setData($data)

return $this;
}

/**
* Determine whether the email address matches one of the given domains.
*
* @param string $value
* @param string[] $domains
* @return bool
*/
protected function domainMatchesPattern($value, $domains)
{
$domain = explode('@', $value)[1];

return Str::of($domain)->is($domains, true);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return Str::of($domain)->is($domains, true);
return Str::of($value)->after('@')->is($domains, true);

}
}
64 changes: 64 additions & 0 deletions tests/Validation/ValidationEmailRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,70 @@ public function testValidationMessages()
);
}

public function testDomainIs()
{
$this->passes(
(new Email())->domainIs(['example.com', 'test.com']),
'passes@example.com',
);

$this->passes(
(new Email())->domainIs(['*']),
'passes@example.com',
);

$this->passes(
(new Email())->domainIs(['*example.com']),
'passes@example.com',
);

$this->passes(
(new Email())->domainIs(['*ex*le.com']),
'passes@subdomain.example.com',
);

$this->passes(
(new Email())->domainIs(['*example.com']),
'passes@subdomain.example.com',
);

$this->passes(
(new Email())->domainIs(['example*']),
'passes@example.com',
);

$this->fails(
(new Email())->domainIs(['test.com']),
'fails@example.com',
['The '.self::ATTRIBUTE_REPLACED.' must be a valid email address.']
);
}

public function testDomainIsNot()
{
$this->passes(
(new Email())->domainIsNot(['test.com', 'example.org']),
'passes@example.com',
);

$this->passes(
(new Email())->domainIsNot(['*test.com']),
'passes@example.com',
);

$this->fails(
(new Email())->domainIsNot(['example.com']),
'passes@example.com',
['The '.self::ATTRIBUTE_REPLACED.' must be a valid email address.']
);

$this->fails(
(new Email())->domainIsNot(['example*']),
'passes@example.com',
['The '.self::ATTRIBUTE_REPLACED.' must be a valid email address.']
);
}

protected function setUp(): void
{
$container = Container::getInstance();
Expand Down