0

After I used group middleware, I am not able to access error messages. Error bags returns empty.

There was no problem before.

I have researched, some users have solved the problem by changing http/kernel.php

\Illuminate\Session\Middleware\StartSession::class, $middlewareGroups to $middleware.

However, It doesn't work for me.

Also $validated = $request->validated(); function doesnt returns validation error. In my CreditcardRequest Class I have attributes, messages, rules functions. If validation fails these messages needs to be shown. previously When validated(); method was running on the controller, it was showing the messages if the form is empty. I have 20 pages all of them working, before middleware grouping.

Creditcard Blade

<div class="messages">
    @if ($errors->any())
        <div class="row  mt-3">
            <div class="col-md-12">
                <div class="alert alert-warning alert-dismissable" role="alert">
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                    <h3 class="alert-heading font-size-h4 font-w400">Error!</h3>
                    @foreach ($errors->all() as $error)
                        <p class="mb-0">{{ $error }}</p>
                    @endforeach
                </div>
            </div>
        </div>
    @endif
</div>

CreditcardRequest

public function attributes()
{
    return [
        'cc_name' => 'CC Owner',
        ..
    ];
}

public function messages()
{
    return [
        'required' => 'Required: :attribute',
        ...
    ];
}

public function rules()
{
    return [
        'cc_name' => 'required|max:128',
    ];
}

Controller

public function doPaySection(CreditcardRequest $request)
  {

      $validated = $request->validated();
      $cc = TRUE;
      if ($cc):
          return redirect('/pay_success')->with('success', 'success');
      else:
          return redirect('/pay_error')->with('error', 'error');
      endif;
  }

web.php

Route::group(['middleware' => ['client.role:guest']], function () {
    Route::get('/login', 'HomepageController@showLogin')->name('login');
    Route::post('/login', 'HomepageController@doLogin');
    Route::post('/register', 'HomepageController@doRegister');
    Route::get('/register', 'HomepageController@showRegister')->name('register');
});

login.blade

@if ($errors->any())
    <div class="row  mt-3">
        <div class="col-md-12">
            <div class="alert alert-warning alert-dismissable" role="alert">
                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h3 class="alert-heading font-size-h4 font-w400">Hata!</h3>
                @foreach ($errors->all() as $error)
                    <p class="mb-0">{{ $error }}</p>
                @endforeach
            </div>
        </div>
    </div>
@endif

Controller

public function doLogin(Request $request)
{
    if (auth()->guard('client')->attempt(['email' => request('email'), 'password' => request('password')])) {
        return redirect()->intended('/');
    } else {
        return redirect()->back()->with('error', 'error');
    }
}
Karl Hill
  • 12,937
  • 5
  • 58
  • 95
tyasird
  • 926
  • 1
  • 12
  • 29
  • Is there a reason you're not using the auth that comes with Laravel? The code you have at the minute won't add an "error" to the `error bag`. – Rwd Jan 11 '20 at 11:54
  • its not only auth error, I have a lot of forms and I am checking all of them with $request->validated(); method. It should be return an error when inputs are empty. I added my another controller for you. – tyasird Jan 11 '20 at 14:21

3 Answers3

4

Can you try using this header in your request. Especially if you are hitting from postman.

Accept:application/json

Before using this, i was getting csrf token in case of invalid requests.

enter image description here

Amaar Hassan
  • 338
  • 2
  • 7
2

The code you have at the minute won't add a message to the $errors MessageBag, it will simply add a value to the session called error.

If you want to add an error to the message bag you could simply throw a ValidationException which redirect back with that message:

public function doLogin(Request $request)
{
    if (auth()->guard('client')->attempt($request->only('email', 'password'))) {
        return redirect()->intended('/');
    }

    throw ValidationException::withMessages([
        'error' => 'The error message',
    ]);
}

Don't forget to import ValidationException with:

use Illuminate\Validation\ValidationException;
Rwd
  • 34,180
  • 6
  • 64
  • 78
  • sir thank you. it worked for login page. but I am still not able to get validation errors. before creating middleware I can show error messages with request->validated(); but now errors returns empty. I edited question btw. – tyasird Jan 11 '20 at 14:19
  • @tyasird The `validated()` method isn't for displaying errors, it's to get the properties from the request that you have validated (included in your Form Request class). When you type hint a Form Request class as an attribute of a controller method the validation will happen automatically, if the validation fails then it won't run a single line in your controller method and will simply redirect back to the previous page with the errors. What error(s) are you expecting to see?? – Rwd Jan 11 '20 at 14:32
  • In my RequestClass I have attributes, messages, rules functions. If validation fails this messages needs to be shown. previously When validated(); method was running on controller, it was showing the messages if the form is empty. I have 20 pages all of them working, before middleware grouping – tyasird Jan 11 '20 at 14:40
  • @tyasird **Specifically** what error message are you expecting to see? – Rwd Jan 11 '20 at 14:46
  • I expect to see form input required error. As you can see on my CreditcardRequest class, form input cc_name shouldnt be empty. in the CreditcardRequest class there are error messages and required input names that I expect to validation. The error should returned when input was empty, but now it does not returns any error. validation works there is no problem but I cant see validation errors on blade. – tyasird Jan 11 '20 at 15:00
1

Your will be able to get in session('error')from below

return redirect()->back()->with('errors', 'error');

So your code would be like

@if (session('errors'))
    <div class="row  mt-3">
        <div class="col-md-12">
            <div class="alert alert-warning alert-dismissable" role="alert">
                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h3 class="alert-heading font-size-h4 font-w400">Hata!</h3>
                @foreach (session('errors') as $error)
                    <p class="mb-0">{{ $error }}</p>
                @endforeach
            </div>
        </div>
    </div>
@endif
Niklesh Raut
  • 34,013
  • 16
  • 75
  • 109