Search code examples
htmllaravelemail-validationemail-formats

inconsistent email format of html input field with laravel email validation


In my blade file i have a html form for user in which input field email have type email. But i think it provide inconsistent email format validation. example it show abc@xyz and [email protected] both are correct email address but in my controller, validator method return me invalid email format for abc@xyz. How can i resolve this issue???

In my blade file:

<input type="email" name="email" value="">

And in my controller:

$validator = Validator::make($request->all(), [
     'email'       => 'email|unique:table_name,col_name',
    ],[
     'email.unique' => 'email is already in used',
   ]);

if ($validator->fails()) {
    return redirect()->back()->withInput($request->all())->withErrors($validator);
  }

Solution

  • Actually Laravel using PHP builtin function called filter_var (source code).

    // Illuminate\Validation\Concerns\ValidateAttributes
    
    /**
     * Validate that an attribute is a valid e-mail address.
     *
     * @param  string  $attribute
     * @param  mixed   $value
     * @return bool
     */
    public function validateEmail($attribute, $value)
    {
        return filter_var($value, FILTER_VALIDATE_EMAIL) !== false;
    }
    

    The rules implemented by PHP is quite different with the rules implemented by the browser. abc@xyz passed the browser validation rule because xyz is a valid hostname (you can read it here). But PHP implementing more strict rule that the email address should contains top level domain.