Skip to content
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

[5.8] Implement better email validation support #29589

Merged
merged 2 commits into from Aug 15, 2019
Merged

[5.8] Implement better email validation support #29589

merged 2 commits into from Aug 15, 2019

Conversation

driesvints
Copy link
Member

These changes allow for multiple email validators to be added when checking for valid emails. This is a continuation of the previous PR: #26503

Basically this allows for two things:

  • Make use of multiple email validators provided by the egulias/email-validator package
  • Use the previous (and much requested) filter_var validation

By default nothing's breaking because it'll still use the RFC validator to when no validators are passed to the email validation rule. But you can opt to include different ones or multiple ones:

'email' => 'email:rfc,dns'

Or opt for the pre-5.8 behavior:

'email' => 'email:filter'

Which will use filter_var to validate the email address. This should give people a little more flexibility when doing email validation.

PS: I wasn't sure if we should return something for the getError and getWarnings methods in the FilterEmailValidation class. I've left them with some defaults and everything seems to be working ok.

These changes allow for multiple email validators to be added when checking for valid emails. This is a continuation of the previous PR: #26503

Basically this allows for two things:

- Make use of multiple email validators provided by the egulias/email-validator package
- Use the previous (and much requested) filter_var validation

By default nothing's breaking because it'll still use the RFC validator to when no validators are passed to the email validation rule. But you can opt to include different ones or multiple ones:

    'email' => 'email:rfc,dns'

Or opt for the pre-5.8 behavior:

    'email' => 'email:filter'

Which will use `filter_var` to validate the email address. This should give people a little more flexibility when doing email validation.
@driesvints driesvints changed the title Implement better email validation support [5.8] Implement better email validation support Aug 15, 2019
@taylorotwell taylorotwell merged commit ff86631 into laravel:5.8 Aug 15, 2019
@driesvints driesvints deleted the improve-email-validation branch August 15, 2019 16:24
@tillkruss
Copy link
Collaborator

@driesvints: Could we get the "reasons" as errors as well? For example the MX DNS check still just says: "The email must be a valid email address." instead of "No MX or A DSN record was found for this email".

They got a bunch of errors and exceptions:

https://github.com/egulias/EmailValidator/blob/master/EmailValidator/Exception/NoDNSRecord.php

@driesvints
Copy link
Member Author

@tillkruss yeah but I won't have time to implement this myself. Feel free to send in a pr.

@tillkruss
Copy link
Collaborator

@driesvints: Their error messages are not that great. I made my own rule.

<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

use Egulias\EmailValidator\EmailValidator;

use Egulias\EmailValidator\Validation\DNSCheckValidation;
use Egulias\EmailValidator\Validation\SpoofCheckValidation;
use Egulias\EmailValidator\Validation\NoRFCWarningsValidation;
use Egulias\EmailValidator\Validation\MultipleValidationWithAnd;

use Egulias\EmailValidator\Warning\NoDNSMXRecord;
use Egulias\EmailValidator\Exception\NoDNSRecord;
use Egulias\EmailValidator\Validation\Error\SpoofEmail;

class ValidEmail implements Rule
{
    protected $validator;

    public function __construct()
    {
        $this->validator = new EmailValidator;
    }

    public function passes($attribute, $value)
    {
        $validations = [
            new NoRFCWarningsValidation,
            new SpoofCheckValidation,
            new DNSCheckValidation,
        ];

        $validation = new MultipleValidationWithAnd(
            $validations,
            MultipleValidationWithAnd::STOP_ON_ERROR
        );

        return $this->validator->isValid($value, $validation);
    }

    public function message()
    {
        $error = $this->validator->getError()->getErrors()[0];

        if ($error instanceof SpoofEmail) {
            return 'The :attribute contains suspicious characters.';
        }

        if ($error instanceof NoDNSMXRecord) {
            return 'The :attribute does not have a valid MX record.';
        }

        if ($error instanceof NoDNSRecord) {
            return 'The :attribute does not have a valid DNS record.';
        }

        return 'The :attribute must be a valid email address.';
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants