1

I am using laravel 4 , and I have a form with checkboxes and on sumbitting the form , it goes through the validation error process, if there is error how do I make it save the post values of these check boxes?

AdminRolesController:

     public function postActions($action = NULL) {

  // Allowed post actions..
  $allowed = array('add', 'edit');
  $action = in_array($action, $allowed) ? $action : NULL; 

  // check if action is not null
  if(is_null($action))
   return Redirect::to('admin/roles');

  else
  {
         // POST ACTION
           if($action == "add")
            {                 
               // put all your rules.
               $rules = array(
                'name'=>'required|regex:/^[a-zA-Z ]*$/|min:2',
                'permission_ids' =>'required'
                );


                // run the validation rules on the inputs from the form
                $validator = Validator::make(Input::all(), $rules); 
                // get all permissions or groups available
                $perms = Permissions::all();

                // share it to the view
                // we have two parts of permissions ( 0 , 1) 
                // 0 : Admin Group Pages , 1: Front End Pages

                View::share('perms', $perms);

                    if ($validator->passes()) 
                    {
                        // validation has passed, save user in DB
                        // create instance of our model..
                        // create a new role
                          $role = new Role;
                          $role->name = Input::get('name');
                          $permission_ids = Input::get('permission_ids');


                          // save info to db.
                          $role->save();

                         $msg = 'Role '.$role->name.' has been added'; 
                    }// end validation if
                    else 
                    {
                    // validation has failed, display error messages 
                    return Redirect::back()->with('message', 'The following errors occurred:')->withErrors($validator)->withInput();  
                    }

            }// end if add

   }
 }

I think part of the problem me redirecting with error messages , all the post values is lost , how could I still keep them?

Thanks

1 Answer 1

1

Your controller looks fine - all that's required to do to pass input back to the view is chaining ->withInput()

However, in your views, ensure you're populating the form using the old input values. You can do so, using Blade, by doing something like:

{{ Form::checkbox('permission_id', 'value', Input::old('permission_id)) }}
Sign up to request clarification or add additional context in comments.

3 Comments

but i dont have Username? I have permission_ids as an array in checkboxes
I updated my answer - what I had before was just an example. Basically, just set the value of the checkbox in your form to Input::old('name_of_checkbox')
could I make it an array if I have more than one checkboxes? like : {{ Form::checkbox('permission_id[]', 'value', Input::old('permission_id)) }}

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.