0

I am trying to get validation errors to display in my application and have run into a wall.

I have tried this a few different ways, this is how I have it setup currently...

My routes look like this:

<?php

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

    Route::get('/', [
        'as'   => 'home',
        'uses' => 'HomeController@index'
    ]);

    Route::post('/mailing', [
        'as' => 'mailing.create',
        'uses' => 'MailingController@create'
    ]);

});

My mailing controller looks like this:

class MailingController extends Controller
{
    public function create(Request $request)
    {
        $this->validate($request, [
            'email' => 'required',
        ]);
        dd($request->email);
    }
}

My form looks like this...

            <form action="{{ route('mailing.create') }}" method="post">

                <label for="email">
                    Email
                    <input type="text" name="email" id="email">
                    @if ($errors->has('email'))
                        {{ $errors->first('email') }}
                    @endif
                </label>

                <input type="submit">
                {{ csrf_field() }}
            </form>

When I submit an empty form it goes to the create method in the MailingController however nothing happens, no errors are displayed. Just the form.

Really this should work, could the issue possibly be that the example above was good for Laravel 5.1 but does not work in the latest 5.2 version?

Many thanks in advance for your kind consideration.

2 Answers 2

1

You can see in the official documentation that the web middleware group is applied by default:

Keep in mind, the web middleware group is automatically applied to your default routes.php file by the RouteServiceProvider.

If you execute

 php artisan route:list

You can see that the middleware is applied two times to the web router.

You have to delete the group

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

}

This have changed in a minor 5.2 version, so it is probable that you have this error after a "composer update".

More info here and here.

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

Comments

0

Well it seems I have to answer my own question... in laravel 5.2 the routes are automatically within the middleware route so changing the routes from...

<?php

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

    Route::get('/', [
        'as'   => 'home',
        'uses' => 'HomeController@index'
    ]);

    Route::post('/mailing', [
        'as' => 'mailing.create',
        'uses' => 'MailingController@create'
    ]);

});

to

<?php

    Route::get('/', [
        'as'   => 'home',
        'uses' => 'HomeController@index'
    ]);

    Route::post('/mailing', [
        'as' => 'mailing.create',
        'uses' => 'MailingController@create'
    ]);

Fixed everything.

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.