Drwhite
Drwhite

Reputation: 1685

Why error messages doesn't Appear in Laravel views?

I want to pass custom validation messages to my view using a custom request when storing a role.

I have create a new Request called StoreRoleRequest

<?php

namespace App\Http\Requests;

use App\Http\Requests\Request;
use Illuminate\Contracts\Validation\Validator;

class StoreRoleRequest extends Request
{
    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
            'name'   => 'required'
        ];
    }

    protected function formatErrors(Validator $validator)
    {
        return $validator->errors()->all();
    }

    public function messages()
    {
        return [
            'name.required' => 'the name of the Role is mandatory',
        ];
    }
}

And then pass this custom Request to my store function in the RoleController like this:

public function store(StoreRoleRequest $request)
{
    Role::create($request->all());
    return redirect(route('role.index'));
}

I have a view that show the create role form where the validation seems to work properly but without showing me error even if i call them into the view like this:

{!! Former::open()->action(route('role.store')) !!}
@if (count($errors->all()))
    <div class="alert alert-danger">
        @foreach ($errors->all() as $error)
            <li>{{ $error }}</li>
        @endforeach
    </div>
@endif
{!! Former::text('name')->label('Groupe name')   !!}
{!! Former::text('display_name')->label('Displayed name')   !!}
{!! Former::text('description')->label('Description')   !!}

{!! Former::actions( Button::primary('Save')->submit(),
                    Button::warning('Clear')->reset()  ,
                    Button::danger('Close')->asLinkTo('#')->withAttributes(['data-dismiss' => 'modal'])
 )!!}
{!! Former::close() !!}

Has anyone an idea why the errors doesn't appear into the view ? am I looping something inside the custom Request ?

EDIT

NB: Even in the login and the registration form the errors doesn't appear anymore.

In this case i have change my middlware that was pointed to web ['middleware' => ['web'] to this:

Route::group(['middleware' => []], function () 
{
    // other routes
    Route::resource('role', 'RoleController');
});

and all my errors displayed perfectly.

have you locate the root cause about this issue ?

Upvotes: 0

Views: 1516

Answers (2)

Marcin Nabiałek
Marcin Nabiałek

Reputation: 111829

After your question update it seems, you have newer version of Laravel application (don't confuse it with Laravel framework). To verify this, open file app/Providers/RouteServiceProvider.php and verify method what's the content of map method. In case it launches mapWebRoutes it means that you have 5.2.27+ application which applies web group middleware automatically.

In case web middleware is applied automatically you shouldn't apply web middleware in your routes.php file because it will cause unexpected behaviour.

So you should either remove web middleware from your routes.php in case you have mapWebRoutes defined in your RouteServiceProvider class or you can modify your RouteServiceProvider class to not apply web group middleware automatically. It's up to you which solution you choose.

Just for quick reference:

Upvotes: 1

Try to ask if errors exists by this way:

@if($errors->any())
    // Your code
    @foreach($errors->all() as $error)
        <li>{{ $error }}</li>
    @endforeach
    // More code
@endif

Also remove the formatErrors function from the request... You don't need it...

The function messages() is responsible for returning your custom messages...

Regards.

Upvotes: 0

Related Questions