Reputation: 1536
In my controller there are a bunch of validations. After validating them I check if a certain element is present in the session. If that element is absent then I send another error. I want to show all the validation error and other error together.
$this->validate($request,[
'other11' => 'nullable|image',
'other12' => 'nullable|image',
'other13' => 'nullable|image',
'other14' => 'nullable|image',
'other15' => 'nullable|image',
]);
if(session()->get('media')['other10']==NULL)
{
return back()->withErrors(['other10'=>'No data in session']);
}
Currently, if there is a validation error then the error regarding 'other10' field is not displayed in the view. Is there a way to return both the validation error and the error regarding 'other10' together to the view?
Upvotes: 0
Views: 4942
Reputation: 39
$this->validate($request,[
'other11' => 'nullable|image',
]);
This will redirect back if any error message, validation fails. After that, print messages in view like this:
@if ($errors->has('other11'))
{{ $errors->first('email') }}
@endif
If you want to print all messages, this will help you:
@if($errors->has())
@foreach ($errors->all() as $error)
<div>{{ $error }}</div>
@endforeach
@endif
Better to use Laravel Form Request validation snipped code from Laravel:
public function rules()
{
return [
'title' => 'required|unique:posts|max:255',
'body' => 'required',
];
}
Upvotes: 0
Reputation: 1011
Create a validator instance with all the validation rules, then you can take its errors and add as many errors as you want to it. It's something like the following:
$validator = Validator::make($request->all(), [
'other11' => 'nullable|image',
'other12' => 'nullable|image',
'other13' => 'nullable|image',
'other14' => 'nullable|image',
'other15' => 'nullable|image'
]);
$errors = $validator->errors();
if (session()->get('media')['other10'] == NULL) {
$errors->add('other10', 'No data in session');
}
return back()->withErrors($errors);
Upvotes: 3
Reputation: 646
use
return redirect()->back()->with('error' ,'error message');
instead of
return back()->withErrors(['other10'=>'No data in session']);
Upvotes: 0