我正在使用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
}
}
我认为部分问题是我使用错误消息重定向,所有帖子值都丢失了,我怎么能保留它们呢?
由于
答案 0 :(得分:1)
您的控制器看起来很好 - 将输入传递回视图所需要做的就是链接->withInput()
但是,在您的视图中,请确保使用旧输入值填充表单。您可以使用Blade执行此操作,例如:
{{ Form::checkbox('permission_id', 'value', Input::old('permission_id)) }}