11

As you may expect, when validation fails, I created a redirect.

return Redirect::to('search')->withErrors($v->messages()) 

I can access it in the view with out a problem, but I would like to do something different. I have an ErrorPartial.blade.php, which I would like to be passed to my search view.

return View::make('search.searchForm') ->with('title', 'Search Page') ->with('components', Subject::select('Component')->distinct()->get()) ->with('measurementRow',$measurementRow) ->with('races', Race::parseRaceTable()) ->with('errorPartial', View::make('errorPartial') ->with('errors',$v->messages()) ->render()) ; 

The Problem is I don't have access to $v in this controller function. Can I access the errors that are going to be passed to the view some how? I've tried this:

return Redirect::to('search')->withErrors($v->messages()) ->with('v', $v); 

But I get this error.

Serialization of 'Closure' is not allowed 

I could just create the Partial view in my search view but I was wondering if their was a way to do it this way. If anyone knows which would be more efficient or GPP then I wouldn't mind knowing that as well.

Thanks

4
  • Include the partial from the view. When you pass errors they will be available to the partial as well. Commented Apr 17, 2014 at 16:22
  • Is this the efficient way to do it/ do you know if it even possible to do it the other way, accessing the errors in the controller and passing the view a partial view. And do you know if just creating the partial in the view is GPP (Good programming practice) Commented Apr 17, 2014 at 17:14
  • 1
    As long as the view or partial doesn't know where data is coming from, like calling a model within them, you should be fine regarding GPP. Please take a look in a previous question of mine, stackoverflow.com/questions/16092045/… Commented Apr 17, 2014 at 17:42
  • Your comment lead me to laravel.com/docs/responses which lead me to laravel.com/docs/session . In my Controller I can do this: $errors = Session::get('errors'), allowing me to pass it to a partial view. TY Commented Apr 17, 2014 at 20:55

3 Answers 3

13

Laravel stores the errors in the Session.

And all the functions available in blade are coming from ViewErrorBag class.

use Illuminate\Support\ViewErrorBag; ... $errors = session()->get('errors', app(ViewErrorBag::class)); 

This approach is preferred because it returns empty error bag if there is no errors in the session. It means that you can call $errors->any() on this object without expecting to have an error saying

Call to a member function any() on null

You can always find an implementation in source code following this advice

The $errors variable is bound to the view by the Illuminate\View\Middleware\ShareErrorsFromSession middleware, which is provided by the web middleware group. When this middleware is applied an $errors variable will always be available in your views, allowing you to conveniently assume the $errors variable is always defined and can be safely used.

Documentation

Sign up to request clarification or add additional context in comments.

1 Comment

Great answer as it's "The Laravel Way." This should be the accepted answer.
11

Answering the question to close it as an open question.

Laravel stores the errors in the Session, which can be access like so:

$errors = Session::get('errors'); 

1 Comment

Not quite. You cannot call $errors->any() is there is not errors, you will get Call to a member function any() on null when in the blade this will work without errors.
0

Here is a another option. That gets the error key from the default bag of messages.

use Illuminate\Support\Facades\Session; public function tranfers(Request $request) { ... // test the error exists $has_tranfer_error = (Session::get('errors') && Session::get('errors')->getBag('default')->has('insufficient_funds')); ... } public function make_transfer(Request $request) { ... // Add the error return back()->withErrors(['insufficient_funds' => 'The balance is too low to make a transfer'])->withInput(); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.