0

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?

3 Answers 3

3

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);
Sign up to request clarification or add additional context in comments.

Comments

0

use

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

instead of

return back()->withErrors(['other10'=>'No data in session']);

Comments

0
$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',
    ];
}

3 Comments

I know how to display error messages. I want to send both errors together. For example, if there is a validation error then I can't show the error regarding other10 field.
$errors = $validator->errors(); if (session()->get('media')['other10'] == NULL) { $errors->add('other10', 'No data in session'); } this will restrict to send specific error messages , dont need to return redirect back , laravel automatically redirect you back , if any validation fails
This issue is solved thanks to "Ahmed Nour Jamal El-Din". But for the sake of future, please show me how I can write the logic regarding the 'other10' field inside the "validate" method.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.