Reputation: 1070
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">×</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">×</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');
}
}
Upvotes: 0
Views: 7484
Reputation: 378
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.
Upvotes: 4
Reputation: 35170
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;
Upvotes: 2
Reputation: 34914
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">×</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
Upvotes: 1