数组无效的表单验证

时间:2015-03-11 14:06:36

标签: codeigniter

在我的表格上,我有一些复选框。他们是一个阵列。当我因某种原因提交表单时,它会清除修改复选框。然后回去看看没有检查。

我知道造成问题的原因是$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();?>

1 个答案:

答案 0 :(得分:0)

经过半个小时的努力试图找出答案后,回调的主要部分不在于 set_rules() 权限[修改]

这里只修复了代码以便回拨,现在似乎正在运作。

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;

    }
}