There is this type of Form Request:

public function rules() { return [ 'login' => 'required|max:255|alpha_dash', 'pass' => 'required|min:6|max:255|same:pass_conf', 'pass_conf' => 'required', ]; } public function messages() { return [ 'login.required' => 'Поле ввода не должно быть пустым или иметь пробелы', 'login.max' => 'Логин не может содержать больше 255 символов', 'login.alpha_dash' => 'Поле можно содержать только алфавитные символы, цифры, знаки подчёркивания "_" и дефисы "-"', 'pass.required' => 'Поле ввода не должно быть пустым или иметь пробелы', 'pass.min' => 'Пароль должен содержать не менее 6 символов', 'pass.max' => 'Пароль не может содержать больше 255 символов', 'pass.same' => 'Пароли не совпадают!', 'pass_conf.required' => 'Поле ввода не должно быть пустым или иметь пробелы', ]; } 

This is how the forms and the error output from the Form Request are organized:

  <form class="form-horizontal" method="POST" action="{{ route('setusername') }}" > {{ csrf_field() }} <div class="form-group"> <label for="login" class="control-label">Изменить текущий логин</label> <input type="text" class="form-control mr-sm-2 mb-2" id="login" name="login" placeholder="Введите новый логин"> <button type="submit" class="btn btn-primary">Изменить</button> </div> @if ($errors->any()) {{-- Вывод сообщений о некорректном вводе --}} <div class="alert alert-danger inline-block mt-1"> <ul> @foreach ($errors->get('login') as $message) <li>{{ $message }}</li> @endforeach </ul> </div> @endif </form> 

Controller methods:

 public function setusername(ProfileValidationRequest $request) { $username = $request->login; Auth::user()->name = $username; Auth::user()->save(); return redirect('/profile'); } public function setpassword(ProfileValidationRequest $request) { $password = Hash::make($request->pass); Auth::user()->password = $password; Auth::user()->save(); return redirect('/profile', $password); } 

Routes:

 Route::post('profile.setusername', 'ProfileController@setusername')->name('setusername'); Route::post('profile.setpass', 'ProfileController@setpassword')->name('setpass'); 

The problem is that with such a condition, when sending any form to all the others, error messages also appear. In general, help is needed with the design of the conditions so that error messages appear only on the submitted form.

ps As it turned out, the validation also fails, yielding an empty div alert

    1 answer 1

    To implement error output only for a specific form, you need to do the following:

    in your ProfileValidationRequest add the following class variable:

     protected $errorBag = 'login'; 

    After this, errors only for this form in the templates will be available in the variable

     $errors->login 
    • I apologize, I don’t understand where to add protected $errorBag = 'login'; in Form Request, to some of the methods, or just to a validator class? - Warden
    • in ProfileValidationRequest. class variables are added simply to the class) - P. Fateev February