Ad

Laravel 5.0 How To Add A Custom Check Before Registration

- 1 answer

I am working a Laravel project (Laravel 5.0) and came across a roadblock. Basically what I am trying to do is make it so that only people with a certain email domain (let's say @gmail.com as an example) to register. I have looked through almost all of the files to do with registration and can't seem to find where to add a check.

The code is really simple:

if (substr($email,-10,10)!="@gmail.com") { 
    echo "we have an issue here";
}

Now, preferably, it wouldn't just echo an error, but rather include it in the list of errors such as when you leave the name field blank.

If you know how to do this, please post some code and the file to put said code in.

Thanks again!

P.S. I am using all of the default authentication built into Laravel 5.0 to do my registration and login.

Ad

Answer

First thing you need to do is create a custom validator There are several ways to make a custom validator in Laravel but I prefer doing it like so:

  1. Firstly create a custom validator class, this will serve as a place where you can register all your custom validators in the future, Mine is located in app\customs - I created customs directory as it doesn't exists.

    <?php namespace App\Customs;
    
    use Illuminate\Validation\Validator;
    
    class Validators extends Validator
    {
    
          public function validateGoodEmail($attribute, $value, $parameters)
          {
             return (substr($value,-10,10)!="@gmail.com"); 
             //return boolen - true or false
          }
    
    }
    

NB: Notice the keyword in the method namevalidate example validateGoodEmail, validateSex etc.

  1. Updated: Next up, create a ValidatorServiceProvider.php in app\providers and do something similar to this:

    <?php namespace App\Providers;
    
    use Illuminate\Support\ServiceProvider;
    
    use Validator;
    use App\Customs\Validators;
    
    class ValidatorServiceProvider extends ServiceProvider
    {
    
          public function boot()
          {
               Validator::resolver(function($translator, $data, $rules, $messages){
                    return new Validators($translator, $data, $rules, $messages);
               });
          }
    
    
          public function register()
          {
    
          }
    }
    
  2. Register the newly created service provider in config/app.php under providers

     'App\Providers\ValidatorServiceProvider',
    
  3. To use the validator in default laravel registration process, pop open app\services\Registrar.php and add this

NB: In Laravel 5.2 checkout app\http\controllers\auth\AuthController.php

use App\User;
use Validator;
use Illuminate\Contracts\Auth\Registrar as RegistrarContract;
use Mail;

class Registrar implements RegistrarContract {

    public function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|max:255',
            'email' => 'required|good_email|email|max:255|unique:users',
            'password' => 'required|confirmed|min:6',
        ]);
    }
}

Updated:For a quick fix, you can skip step 1 - 4 and simply modify app\services\Registrar.php like so:

public function validator(array $data)
{
     \Validator::extend('good_email', function($attribute, $value, $parameters){
             return (substr($value,-10,10)!="@gmail.com"); 
      });

      return \Validator::make($data, [
            'name' => 'required|max:255',
            'email' => 'required|good_email|email|max:255|unique:users',
            'password' => 'required|confirmed|min:6',
        ]);
 }
  1. One last thing to do is add good_email in resources\lang\en\validation.php

    'unique'               => 'The :attribute has already been tak
    'url'                  => 'The :attribute format is invalid.',
    
    ......                                                               
    'good_email'       => 'Sorry :attribute must end with "@gmail.com"',   
    ......
    
Ad
source: stackoverflow.com
Ad