2

Im migrating a cms from 5.7 to latest, and i havent been working with laravel for 3 years.

i have an error: in a nutshell: i have a form, i have a controller with save action, validation rules & messages and validation. Now the validator seems to be working (fails), and redirects me to the prev. rout, BUT the $errors var is always empy and the old form values are not stored.

Posting a valid form works ok.

i paste the relevalt stuff below:

ROUTS

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



        Route::group(['prefix' => 'roles'], function () {
            Route::get('/', 'BE\RolesController@getIndex' )               ->name('adm_roles_index');
            Route::get('/add', 'BE\RolesController@addAction' )           ->name('adm_roles_add');
            Route::get('/{role}', 'BE\RolesController@editAction')        ->name('adm_roles_edit');
            Route::post('/save', 'BE\RolesController@submitAction')       ->name('adm_roles_save');
        });

/// some more routs ehere
}

Controller (relevant stuff)

 public function addAction(Request $request)
    {
        return $this -> addPageAction($request);
    }

    public function editAction(Role $role, Request $request)
    {
        return $this -> addPageAction($request, $role);
    }

    private function addPageAction(Request $request, $role = null)
    {

        $mode            = 'edit';
        $_userRoles = array();
        $activationStatus = false;
        $roles = false;

        if (!$role)
        {
            $mode = 'add';
        }
        else
        {
            if ($role->id == 1) { // editing the supervisor is not allowed, redirect to list page
                return redirect( route('adm_roles_index') );
            }
        }

        return view('BE.roles.edit') -> with([
            'action'          => $mode,
            'data'       => $role,
        ]);
    }

    public function submitAction(Request $request)
    {
        $messages = [
            'name.required' => 'The name is required.',
            'name.unique' => 'The name is already in use.',
            'slug.required' => 'The slug is required.',
            'slug.unique' => 'The slug is already in use.',
        ];

        $validatorRules = array(
            'name' => 'required|unique:roles',
            'slug' => 'required|unique:roles',
        );

        if ( !empty($request->id) ) {
            $role = Sentinel::findRoleById($request->id);

            if ($role) {
                $validatorRules['name'] = 'required|unique:roles,name,' . $role->id;
                $validatorRules['slug'] = 'required|unique:roles,slug,' . $role->id;
            }

        }

        $this -> validate($request, $validatorRules, $messages);

       /// some more stuff down here for storing the roles etc. but not so important
}

Blade

    {{ dump($errors) }}

            {!! Form::open(array('url' => route('adm_roles_save'), 'method' => 'post', 'class' => 'form-horizontal', 'id' => 'Form', 'name' => 'Form', )) !!}


          {!! Form::label('name', 'Name', []) !!}
          {!! Form::text('name', old('name', (!empty($data -> name)) ? $data -> name : null ),
                                                    [ 'placeholder' => 'Name',
                                                      'class' => ($errors->has('name') ? 'form-control is-invalid' : 'form-control' )
                                                    ]
                                                ) !!}

{!! Form::close() !!}

This blade always shows

Illuminate\Support\ViewErrorBag {#776 ▼
  #bags: []
}
15
  • 1
    Check like this on your blade @if ($errors->any()) <div class="alert alert-danger"> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif Commented Aug 2, 2020 at 8:15
  • no success.... posting an empty form shows me nothing => validation IS running, because im redirected, but the errors are not passed.... i guess Commented Aug 2, 2020 at 8:23
  • The function is submitAction? Commented Aug 2, 2020 at 8:25
  • processing the POST? yes. if i dd() the request before validation, the values are there (empty or npot, depending on the form of course). if i submit an invalid form the validation runs and the redirection happens, because making a dd('xxx'); after validation will not be executed. if a valid form is posted, the validation passes and the func. continues. Commented Aug 2, 2020 at 8:28
  • Actually the problem is, you make the validation. But not return anything. Where it will return? Commented Aug 2, 2020 at 8:46

3 Answers 3

4

I would suggest checking the result of your validation from inside your controller. you can use Validator::make() to prevent the automatic redirect that ->validate() causes when validation fails.

$validator = Validator::make($request->all(), $validatorRules);
if ($validator->fails()) dd($validator->errors());
Sign up to request clarification or add additional context in comments.

1 Comment

thanks, but already did it. in the controller the validation is fine. it seams that the error and old inpit is not passed between views, although the middleware is there...
4

go to Http/Kernel.php

move this line: \Illuminate\Session\Middleware\StartSession::class

from $middlewareGroups array to $middleware array.

DONE!

1 Comment

Pefect solution, Can you explain why it was happening?
0

I was having this issue, in my case I was using the cookie session driver and my old form values / errors were seemingly too large to keep in the cookie.

Switching to a different session driver resolved this for me.

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.