1

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.

Jake Opena
1,5451 gold badge11 silver badges18 bronze badges
asked May 20, 2015 at 1:22
1
  • You're using Laravel 4 or 5? Commented May 20, 2015 at 1:31

1 Answer 1

3

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.

answered May 20, 2015 at 1:37
Sign up to request clarification or add additional context in comments.

2 Comments

Would i add that Validator::extend in the validator function? @HieuLe
You should put this code inside a Service Provider in Laravel 5, see this post.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.