我的表格上有一些复选框。它们在一个数组中。当我出于某种原因提交表单时,它会清除“修改”复选框。然后回去看没有检查。 我知道导致问题的原因是
$this->form_validation->set_rules('permission[modify]', '', 'callback_modify_check_edit')
它似乎不喜欢,权限[修改]
或权限[修改][]在set_rules
我该如何解决这个问题?控制器编辑功能:
public function edit() { $this->load->library('form_validation'); $this->form_validation->set_rules('name', 'User Group Name', 'required'); $this->form_validation->set_rules('user_group_id', 'User Group Id', 'required'); $this->form_validation->set_rules('permission[modify]', '', 'callback_modify_check_edit'); if ($this->form_validation->run($this) == FALSE) { $this->getForm(); } else { $this->load->model('admin/user/model_user_group'); $this->model_user_group->editUserGroup($this->uri->segment(4), $this->input->post()); $this->db->select('permission'); $this->db->from($this->db->dbprefix . 'user_group'); $this->db->where('user_group_id', $this->session->userdata('user_group_id')); $user_group_query = $this->db->get(); $permissions = unserialize($user_group_query->row('permission')); $this->session->set_userdata($permissions); $this->session->set_flashdata('success', 'Congratulations you have successfully modified' .' '. "<strong>" . ucwords(str_replace('_', ' ', $this->router->fetch_class())) .' '. $this->input->post('name') . "</strong>"); redirect('admin/users_group'); } } public function modify_check_edit() { if (!in_array('users_group', $this->session->userdata('modify'))) { $this->form_validation->set_message('modify_check_edit', 'You do not have permission to edit' ); } }
查看表格:
<?php echo validation_errors('<div class="alert alert-warning text-center"><i class="fa fa-exclamation-triangle"></i> ', '</div>'); ?> <?php if ($this->uri->segment(4) == FALSE) { ?> <?php $data = array('class' => 'form-horizontal', 'id' => 'users_group');?> <?php echo form_open('admin/users_group/add', $data);?> <?php } else { ?> <?php $data = array('class' => 'form-horizontal', 'id' => 'users_group');?> <?php echo form_open('admin/users_group/edit' .'/'. $this->uri->segment(4), $data);?> <?php } ?> <div class="form-group"> <?php $data = array('class' => 'col-sm-2 control-label');?> <?php echo form_label('User Group Name', 'name', $data);?> <div class="col-sm-10"> <?php $data_user_name = array( 'id' => 'name', 'name' => 'name', 'class' => 'form-control', 'value' => $name ) ;?> <?php echo form_input($data_user_name);?> </div> </div> <div class="form-group"> <?php $data = array('class' => 'col-sm-2 control-label');?> <?php echo form_label('User Group Id', 'user_group_id', $data);?> <div class="col-sm-10"> <?php $data_user_group_id = array( 'id' => 'user_group_id', 'name' => 'user_group_id', 'class' => 'form-control', 'value' => $user_group_id ) ;?> <?php echo form_input($data_user_group_id);?> </div> </div> <div class="form-group"> <?php $data = array('class' => 'col-sm-2 control-label');?> <?php echo form_label('Access Permission', 'permission_access', $data);?> <div class="col-sm-10"> <div class="well well-sm" style="height: 200px; overflow: auto;"> <?php foreach ($permissions as $permission) { ?> <div class="checkbox"> <label> <?php if (in_array($permission, $access)) { ?> <?php $data_checked = array( 'name' => 'permission[access][]', 'id' => 'permission_access', 'value' => $permission, 'checked' => TRUE, ); echo form_checkbox($data_checked); ?> <?php echo $permission; ?> <?php } else { ?> <?php $data_not_checked = array( 'name' => 'permission[access][]', 'id' => 'permission_access', 'value' => $permission, 'checked' => FALSE, ); echo form_checkbox($data_not_checked); ?> <?php echo $permission; ?> <?php } ?> </label> </div> <?php } ?> </div> <a onclick="$(this).parent().find(':checkbox').prop('checked', true);">Select All</a> / <a onclick="$(this).parent().find(':checkbox').prop('checked', false);">Unselect All</a></div> </div> <div class="form-group"> <?php $data = array('class' => 'col-sm-2 control-label');?> <?php echo form_label('Modify Permission', 'permission_modify', $data);?> <div class="col-sm-10"> <div class="well well-sm" style="height: 200px; overflow: auto;"> <?php foreach ($permissions as $permission) { ?> <div class="checkbox"> <label> <?php if (in_array($permission, $modify)) { ?> <?php $data = array( 'name' => 'permission[modify][]', 'id' => 'permission_modify', 'value' => $permission, 'checked' => TRUE, ); echo form_checkbox($data); ?> <?php echo $permission; ?> <?php } else { ?> <?php $data = array( 'name' => 'permission[modify][]', 'id' => 'permission_modify', 'value' => $permission, 'checked' => FALSE, ); echo form_checkbox($data); ?> <?php echo $permission; ?> <?php } ?> </label> </div> <?php } ?> </div> <a onclick="$(this).parent().find(':checkbox').prop('checked', true);">Select All</a> / <a onclick="$(this).parent().find(':checkbox').prop('checked', false);">Unselect All</a></div> </div> <?php echo form_close();?>
来实现 权限[修改] 刚刚修复了这里的回调代码,现在似乎可以工作了。
public function modify_check_edit() {
$this->load->library('form_validation');
if (is_array($this->session->userdata('modify'))) {
$permission = !in_array('users_group', $this->session->userdata('modify'));
if ($permission) {
$this->form_validation->set_message('modify_check_edit', 'You do not have permission to one' .' '. "<strong>" . ucwords(str_replace('_', ' ', $this->router->fetch_class())) .' '. $this->input->post('name') . "</strong>");
return FALSE;
} else {
return TRUE;
}
return TRUE;
} else {
$this->form_validation->set_message('modify_check_edit', 'You do not have permission to one' .' '. "<strong>" . ucwords(str_replace('_', ' ', $this->router->fetch_class())) .' '. $this->input->post('name') . "</strong>");
return FALSE;
}
}