Laravel在验证错误时保存帖子变量? [英] Laravel saving post variable when there is validation error?

查看:69
本文介绍了Laravel在验证错误时保存帖子变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用laravel 4,并且我有一个带有复选框的表单,并且在对表单进行加总时,它会经历验证错误过程,如果有错误,我该如何保存这些复选框的post值?

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

   }
 }

我认为问题的一部分是我用错误消息重定向,所有发布值都丢失了,我怎么还能保留它们?

谢谢

解决方案

您的控制器看起来不错-将输入传递回视图所需要做的就是链接->withInput()

但是,请确保在您的视图中使用旧的输入值填充表单.您可以使用Blade通过以下操作做到这一点:

{{ Form::checkbox('permission_id', 'value', Input::old('permission_id)) }}

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

解决方案

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)) }}

这篇关于Laravel在验证错误时保存帖子变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆