0

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 ?

Drwhite
  • 1,545
  • 4
  • 21
  • 44

2 Answers2

1

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:

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
0

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.

  • Doesn't work it seems that the problem is not on displaying the errors but i think is something related to session ! – Drwhite May 05 '16 at 22:48
  • Is this the only view you are having problem with the error mesaages? – Sebastián Rodriguez May 05 '16 at 22:50
  • I have just begin with server side validation and this is the first one. – Drwhite May 05 '16 at 22:52
  • Try to change your `store` function's header from this `public function store(StoreRoleRequest $request)` to this `public function store(Request $request)`, and then add `dd($request->all())` in the function (before of return statement) and see what your controller is receiving... – Sebastián Rodriguez May 05 '16 at 23:38
  • i used to work with Request and it recieve all inputs() from the view after submiting the form but the issue is about validation it work properly but just problem of displaying the errors ! – Drwhite May 06 '16 at 00:08
  • Try to comment the `messages()` function... Laravel should send the predefined messages... – Sebastián Rodriguez May 06 '16 at 00:12