I am using the default Laravel 5 authentication system. I would like to simply add a check that verifies that the user is using a school email account (I am writing this for my University). So for example I want to make sure they are using @harvard.edu, and not @gmail.com. If they are not using the correct email type, I want to add an error to the $errors variable, and print that along with the the other possible errors on the registration form.
It appears that the actual validation occurs in Registrar.php. I am assuming I will have to add something to the email portion of the validator function.
public function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|confirmed|min:6',
]);
}
The other part I am confused about is where the actually error messages are located.
I am new to Laravel, thanks in advance.
-
You're using Laravel 4 or 5?Hieu Le– Hieu Le2015年05月20日 01:31:29 +00:00Commented May 20, 2015 at 1:31
1 Answer 1
To add a custom validation, you can read the documentation at http://laravel.com/docs/5.0/validation#custom-validation-rules . For example, in you case:
Validator::extend('email_harvard', function($attribute, $value, $parameters)
{
$segments = explode("@", $value, 2);
return end($segments) == "harvard.com";
});
After that, modify your validator rules, change the line:
'email' => 'required|email|max:255|unique:users',
to be:
'email' => 'required|email|email_harvard|max:255|unique:users',
About the error messages, you can read here. In Laravel 5, the validation messages are located at resources/lang/xx/validation.php where xx is the language code.
Explore related questions
See similar questions with these tags.