D
D
dentxt2018-08-12 13:51:02
Laravel
dentxt, 2018-08-12 13:51:02

How to add custom validation when registering in Laravel?

I want to add a field to the Laravel registration form that will require a key that I have given. If the entered key is found in the database table, then the registration is successful, or otherwise - an error.
Basic validation is structured like this:

$validator = Validator::make($data, [
            'name' => 'required|string|max:255',
            'email' => 'required|string|email|max:255|unique:users',
            'password' => 'required|string|min:6|confirmed',
        ]);

How to add here a function that looks for a key and as a result returns the answer T or F - I do not know.
In the "create" method, which writes to the user's database, it does not allow checking in itself. Since the method must in any case enter data into the database, so making a redirect back will not work, because there will be an error.
How to do it humanly?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Konstantin B., 2018-08-12
@Kostik_1993

Every day the questions are getting dumber and dumber. If you at least opened the dock and thought a little with your brains, you would understand that you just need to add one more line to the array with the name of your field and the validation rules 'key' => 'required|exists: table name with keys ,
column name ' to the documentation so as not to be dishonored anymore

D
Danil Sapegin, 2018-08-12
@ynblpb_spb

$validator = Validator::make($data, [
      'name' => 'required|string|max:255',
      'email' => 'required|string|email|max:255|unique:users',
      'password' => 'required|string|min:6|confirmed',
   ]);
$validator->after(function ($validator) {
   // проводим проверку на соответствие с БД
   if (....) {
      $validator->errors()->add('key', 'Неправильный ключ');
   }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question