Answer the question
In order to leave comments, you need to log in
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',
]);
Answer the question
In order to leave comments, you need to log in
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
$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 questionAsk a Question
731 491 924 answers to any question