1

I'm getting a problem printing $errors on Blade view. I was using validator on controller/model and everything was ok, the blade could print $errors. But now i want to migrate that validation to Form requests.

If i use json/ajax (application/json) everything works fine, all errors came in response. But when i use form-data (multipart/form-data) the $errors variable is always empty.

What am i missing? Here's some code:

View:

{{ Form::open(['url' => 'foo','files' => true, 'name'=>'foo-form']) }}
    //some inputs
{{Form::close()}}

Form Request:

public function authorize()
{
   return true;
}

public function rules()
{
   return [
       "id"   => "required|exists:foo,id",
       "begin_date"  => "required|date",
       "end_date"    => "required|date|after:begin_date"
   ];
}

[EDIT]

I've already tried many ways to display my errors:

-the official way: https://laravel.com/docs/5.4/validation#quick-displaying-the-validation-errors

-With var_dump and dd

-My way (that work with validator on model/controller):

@if(Session::has('errors'))
  <div class="alert alert-danger alert_header">
    @foreach(Session::get('errors') as $error)
      <p>{{ $error }}</p>
    @endforeach
  </div>
@endif

I've already tested with browser (chrome) and postman too

[EDIT]

Controller:

public function uploadFoo(StoreUploadFoo $request)
{
  return "Foo without errors";
}

Laravel version: 5.4

13
  • Just to verify, Are you submitting the form data through Ajax? Commented Mar 23, 2017 at 12:30
  • @eeya no, i want to use submit form Commented Mar 23, 2017 at 12:31
  • Does your route go trough the web middleware? Commented Mar 23, 2017 at 12:32
  • @TheFallen yes, i am using web middleware Commented Mar 23, 2017 at 12:34
  • @AmrAly the form puts it by himself, but, i've already test it adding on form array Commented Mar 23, 2017 at 12:37

1 Answer 1

0

You might need to set a session in your controller in order for you to return the value you need in your $error variable.

public function uploadFoo(StoreUploadFoo $request)
{
    Session::flash('errors', array('id' => 'ID required', 'begin_date' => 'Begin Date required', 'end_date' => 'End Date'));
    return redirect('/url/to/your/view');
}

Then in your view:

@if(Session::has('errors'))
<div class="alert alert-danger alert_header">
    @foreach(Session::get('errors') as $error)
       <p>{{ $error }}</p>
    @endforeach
</div>
@endif

Hope this helps your case.

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

4 Comments

This doesn't work, because with errors on Form Request (in this example, "StoreUploadFoo"), the function will not execute. In my example, the return string on controller never works (if have errors on Form Request). Said that, if i have errors on validator (Form request), your example of controller will not execute
What kind of error was it? Can you show us that error message? Maybe theres something about the StoreUploadFoo that makes that kind of error?
i've solved the problem, was on middleware auth/web. Ty for help :)
Noted. Sorry about that.

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.