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

Conversation

@driesvints
Copy link
Member

commented Aug 15, 2019

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.

Implement better email validation support
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

2 checks passed

continuous-integration/styleci/pr The analysis has passed
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details

@driesvints driesvints deleted the driesvints:improve-email-validation branch Aug 15, 2019

@tillkruss

This comment has been minimized.

Copy link
Member

commented Aug 21, 2019

@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

This comment has been minimized.

Copy link
Member Author

commented Aug 22, 2019

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

@tillkruss

This comment has been minimized.

Copy link
Member

commented Aug 22, 2019

@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
Projects
None yet
3 participants
You can’t perform that action at this time.