0

I want to redirect to view if a certain condition is not met. Check code:

if($request->get('files') == 'yes' && $request->file('file_name') == null){
                return Redirect::to('brief/'.$id."/edit")
                ->withErrors('errors',"Mention file")
                ->withInput();
            }

It prints array as:

Illuminate\Support\ViewErrorBag {#239
  #bags: array:1 [
    "Mention file" => Illuminate\Support\MessageBag {#240
      #messages: array:1 [
        0 => array:1 [
          0 => "errors"
        ]
      ]
      #format: ":message"
    }
  ]

At view end I am doing:

@if (count($errors) > 0)
                <div style="margin-top: 10%;" class="alert alert-danger">
                    <ul>
                        @foreach ($errors->all() as $error)
                            <li>{{ $error }}</li>
                        @endforeach
                    </ul>
                </div>
            @endif

It is not printing the message Mention File

3
  • Do you have a code like Validator::make($inputs, $rules)? Commented Sep 9, 2015 at 6:42
  • @aldrin27 Yes I have but since this code is not like part of main validator so I am redirecting it separately. Commented Sep 9, 2015 at 6:44
  • Can I see how you validate your data's? Commented Sep 9, 2015 at 6:46

2 Answers 2

1

Either you pass a string with ->withErrors, or pass an array with a key "errors" using ->with:

if($request->get('files') == 'yes' && $request->file('file_name') == null){
            return Redirect::to('brief/'.$id."/edit")
            ->withErrors("Mention file")
            ->withInput();
        }

or

if($request->get('files') == 'yes' && $request->file('file_name') == null){
            return Redirect::to('brief/'.$id."/edit")
            ->with(["errors" => "Mention file"])
            ->withInput();
        }
Sign up to request clarification or add additional context in comments.

Comments

0

Ok I found the issue.

From the documents:

you may use the withErrors method to flash the error messages to the session. When using this method, the $errors variable will automatically be shared with your views after redirection,

So ->withErrors('errors',"Mention file") is actually converting $errors into a string which messing things up. Doing simply ->withErrors("Mention file") just added the message in ErrorBag. So the correct code should be:

if($request->get('files') == 'yes' && $request->file('file_name') == null){
                return Redirect::to('brief/'.$id."/edit")
                ->withErrors("Mention file")
                ->withInput();
            }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.