简体   繁体   中英

Laravel validation errors displaying

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.

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM