0

I can't figure put why laravel blade doesn't catch my error validation and doesn't pass it to my view. In detail

  • I do have error snippet in my blade template
  • below is my validation which works correctly

What I'm missing?

Thank you

This is json message I see instead of message in blade template

{ message: "The given data was invalid.", status_code: 500 }

This snippet I use to let user know about error

@if(count($errors))
<div class="form-group">
    <div class="alert alert-danger">
        <ul>
            @if($errors->all())
                @foreach($errors->all() as $error)
                    <li>{{$error}}</li>
                @endforeach
            @endif
        </ul>
    </div>
</div> @endif

And finally this is my correctly working validation

        $request->validate([
        'email' => 'required|email|unique:subscribers|max:255',
    ]);

EDIT:

This is the rout in web.php

Route::post('saveemail', 'SaveSubscriberEmailController@saveEmail');

And this is the method

namespace App\Http\Controllers;


use App\subscriber;

use Carbon\Carbon;

use Illuminate\Http\Request;

use Ramsey\Uuid\Uuid;


class SaveSubscriberEmailController extends Controller
{

    public function saveEmail(Request $request)
    {
        $request->validate([
            'email' => 'required|email|unique:subscribers|max:255',
        ]);



        $uuid = Uuid::uuid4();

        $subscriber = new subscriber();
        $subscriber->email = $request->email;
        $subscriber->uuid = $uuid->toString();
        $subscriber->created_at = Carbon::now();
        $subscriber->save();

        flash('Registration conformation email has been sent. Please check your mailbox. Thank you!')->success();
        return redirect()->back();
    }
}
4
  • Your route should be inside the web middleware or session wont work, this includes your $errors variable Commented Jan 6, 2018 at 21:47
  • added method and route definition. It's in the routes/web.php so it should be inside web middleware as well Commented Jan 6, 2018 at 22:03
  • web.php is automatically in the web middleware. You can check it by calling php artisan routes:list. You should see web next to your route in the Middleware column. Commented Jan 6, 2018 at 22:46
  • it is in the web route,confirmed with php artisan routes:list Commented Jan 6, 2018 at 22:59

3 Answers 3

1

I've had this problem before and the way I was able to fix it was to wrap the routes with a middleware group that includes the middleware \Illuminate\View\Middleware\ShareErrorsFromSession::class. It adds the session's errors to the view.

In your Kernel.php class's protected $middlewareGroups array it can look something like:

'web' => [
    \Illuminate\View\Middleware\ShareErrorsFromSession::class,
    // other middleware
],

Then where you declare your routes you can do:

Route::group(['middleware' => ['web']], function () { 
    Route::post('saveemail', 'SaveSubscriberEmailController@saveEmail'); 
};
Sign up to request clarification or add additional context in comments.

Comments

0

Request validation only send error of 422 not 500 if you are getting this error it's because of something else and the formRequest error bag won't catch this error .

Comments

0

Route::post('saveemail', 'SaveSubscriberEmailController@saveEmail');

Put this route into web middleware. you can do this like

Route::middleware(['web'])->group(function () {

   Route::post('saveemail', 'SaveSubscriberEmailController@saveEmail');

});

Change your controller to this.

class SaveSubscriberEmailController extends Controller
{

public function saveEmail(Request $request)
{



    $validator = validate($request->all(),[
        'email' => 'required|email|unique:subscribers|max:255',
    ]);

   if($validator->fails()){
    return back()->withErrors($validator);
 }



    $uuid = Uuid::uuid4();

    $subscriber = new subscriber();
    $subscriber->email = $request->email;
    $subscriber->uuid = $uuid->toString();
    $subscriber->created_at = Carbon::now();
    $subscriber->save();

    flash('Registration conformation email has been sent. Please check your mailbox. Thank you!')->success();
    return redirect()->back();
}

}

Hope this helps

3 Comments

@Vojta you have to use withErrors, take a look at my answer I have updated this.
unfortunatelly no change. Your code is not executed as if validation fails rest of the method isn't executed.
@Vojta it has nothing to do with dingo API.

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.