-
Notifications
You must be signed in to change notification settings - Fork 11.7k
Str::isEmail() added to check the email valid or not #54577
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
check the email is valid or not using Str::isEmail()
|
Would be nice if this used |
@shaedrich but current code showing me that email is invalid. What do you think? |
|
I guess we should use whichever the default is for validating emails using the validator: https://laravel.com/docs/11.x/validation#rule-email That would be using It would be odd if a request passes validation for an email, and then Optionally, as the email validation rule allows, we could allow the developer to specify which validation style they want to use. As for I truly don't know all the rules, per RCF this would be a valid email:
That passes with Also, your implementation checks for the characters after the last dot on the domain part to have at least two characters, while the RFC allows emails using an IP:
That would fail, as there is a single character after the last dot. (EDIT: actually, it already fails Of course, those are very rare cases, and I wouldn't allow them in any system I maintain (unless it is a hard-request from an enterprise customer). Nonetheless, again, a developer relying on the default validation could be surprised if TL;DR; my opinion is to be consistent with our current email validation rule. |
using egulias/EmailValidator to check email validate or not
|
egulias/EmailValidator implemented to check email valid or not. Thanks @shaedrich , @rodrigopedra |
shaedrich
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess we should use whichever the default is for validating emails using the validator:
https://laravel.com/docs/11.x/validation#rule-email
That would be using
egulias/email-validatorwith itsRFCValidation.It would be odd if a request passes validation for an email, and then
Str::isEmail()fails
egulias/EmailValidator implemented to check email valid or not. Thanks @shaedrich , @rodrigopedra
Yeah, this way, it's consistent 👍🏻
| public static function isEmail(string $value): bool | ||
| { | ||
| $validator = new EmailValidator(); | ||
|
|
||
| return $validator->isValid($value, new RFCValidation()); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optionally, as the email validation rule allows, we could allow the developer to specify which validation style they want to use.
| public static function isEmail(string $value): bool | |
| { | |
| $validator = new EmailValidator(); | |
| return $validator->isValid($value, new RFCValidation()); | |
| } | |
| public static function isEmail(string $value, ?string $rule = null): bool | |
| { | |
| $validator = new EmailValidator(); | |
| return $validator->isValid($value, match ($rule) { | |
| 'strict' => new NoRFCWarningsValidation(), | |
| 'dns' => new DNSCheckValidation(), | |
| 'spoof' => new SpoofCheckValidation(), | |
| 'filter' => new FilterEmailValidation(), | |
| default => new RFCValidation(), | |
| }); | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of a string, $rule could probably be made an enum 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@shaedrich , i think it must be enum 100%
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The enum could even do the mapping itself:
enum EmailValidation
{
case NoRfcWarnings;
case DnsCheck;
case SpoofCheck;
case Filter;
case Rfc;
public function validation()
{
return match ($this) {
self::NoRfc => new NoRFCWarningsValidation(),
self::Dns => new DNSCheckValidation(),
self::SpoofCheck => new SpoofCheckValidation(),
self::Filter => new FilterEmailValidation(),
default => new RFCValidation(),
}
}
}| public static function isEmail(string $value): bool | |
| { | |
| $validator = new EmailValidator(); | |
| return $validator->isValid($value, new RFCValidation()); | |
| } | |
| public static function isEmail(string $value, ?EmailValidation $rule = null): bool | |
| { | |
| $validator = new EmailValidator(); | |
| return $validator->isValid($value, $rule?->validation() ?? new RFCValidation()); | |
| } |
|
Thanks for your pull request to Laravel! Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include. If applicable, please consider releasing your code as a package so that the community can still take advantage of your contributions! |
Check the email is valid or not using Str::isEmail()
I've used here egulias/EmailValidator to check email validation
Now, we can check the email valid or not using Str::isEmail()
if email is valid then it's return true
else it's return false
Example:
It's return true.
Another example:
It's return false.