checkbox []回调不起作用codeigniter

时间:2015-04-05 15:55:39

标签: codeigniter

当我提交表单时,我想使用带回调的codeigniter表单验证集消息。

我的复选框在数组中时出现问题,即name =" selected []"不会提交回拨检查。

它的工作回调验证适用于单个帖子,但没有多个帖子数组。

仍然使用codeigniter表单验证设置消息,这将是实现它的最佳方式,因此消息将显示。

<?php

class User_group_list extends Admin_Controller {

public function index() {
    $data['title'] = ucwords(str_replace('_', ' ', $this->router->fetch_class()));

    $data['breadcrumbs'] = array();

    $data['breadcrumbs'][] = array(
        'text' => 'Home',
        'href' => site_url('admin/dashboard')
    );

    $data['breadcrumbs'][] = array(
        'text' =>  ucwords(str_replace('_', ' ', $this->router->fetch_class())),
        'href' => base_url('admin/user_group')
    );

    $data['user_groups'] = array();

    $results = $this->get_user_groups();

    foreach ($results as $result) {
        $data['user_groups'][] = array(
            'user_group_id' => $result['user_group_id'],
            'name' => $result['name'],
            'edit' => site_url('admin/user_group/update' .'/'.$result['user_group_id'])
        );
    }

    if (isset($_POST['selected'])) {
        $data['selected'] = (array)$_POST['selected'];
    } else {
        $data['selected'] = array();
    }

    $this->load->view('template/user_group/user_group_list', $data);
}

public function get_user_groups() {
    $user_group = $this->db->get($this->db->dbprefix . 'user_group');
    if ($user_group->num_rows()) {
        return $user_group->result_array();
    } else {
        return false;
    }
}

public function delete() {
    $this->load->library('form_validation');

    $selected_post = $this->input->post('selected');

    $this->form_validation->set_rules('selected[]', 'selected', 'required|callback_validateDelete');

    if ($this->form_validation->run($this) == FALSE) {
        redirect('admin/user_group');
    } else {
        $this->session->set_flashdata('success', 'Success: You have modified user groups!');
        redirect('admin/user_group');
    }
}

public function delete_user_group($user_group_id) {
    $this->db->where('user_group_id', $user_group_id);
    $this->db->delete($this->db->dbprefix . 'user_group');
}

public function validateDelete() {
    $this->load->library('user');
    $this->load->library('form_validation');

    if ($this->user->hasPermission('modify', "User_group_list")) {
        return true;
    } else {
        $this->form_validation->set_message('validateDelete', 'Warning: You do not have permission to modify user groups');
        return false;
    }

}

}

查看

<?php echo Modules::run('admin/common/header/index');?><?php echo Modules::run('admin/common/column_left/index');?>
<div id="content">

    <?php $data = array('class' => "form-horizontal");?>
    <?php echo form_open_multipart('admin/user_group/delete', $data);?>

    <div class="page-header">
    <div class="container-fluid">
    <div class="pull-right">
    <a href="<?php echo base_url('admin/user_group/add');?>" role="button" class="btn btn-success"><i class="fa fa-plus"></i></a>
    <button type="submit" class="btn btn-danger"><i class="fa fa-trash"></i></button>
    </div>
    <h1><?php echo $title; ?></h1>
    <ul class="breadcrumb">
    <?php foreach ($breadcrumbs as $breadcrumb) { ?>
    <li><a href="<?php echo $breadcrumb['href']; ?>"><?php echo $breadcrumb['text']; ?></a></li>
    <?php } ?>
    </ul>
    </div>
    </div>

    <div class="container-fluid">
        <div class="panel panel-default">
            <div class="panel-heading clearfix">
                <div class="pull-left">
                <h1 class="panel-title"><?php echo $title;?></h1>
                </div>
                <div class="pull-right">
                </div>
            </div>
            <div class="panel-body">
                <?php echo Modules::run('admin/message/messages/index');?>
                <div class="table-responsive">
                <table class="table table-striped table-bordered table-hover">
                <thead>
                    <tr>
                        <td style="width: 1px;" class="text-center"><input type="checkbox" onclick="$('input[name*=\'selected\']').prop('checked', this.checked);" /></td>
                        <th style="color: #1e91cf;">User Group Name</th>
                        <td class="text-right">Action</td>
                    </tr>
                </thead>
                <tbody>
                    <?php if ($user_groups) { ?>
                    <?php foreach ($user_groups as $user_group) { ?>
                    <tr>
                        <td class="text-center"><?php if (in_array($user_group['user_group_id'], $selected)) { ?>
                        <input type="checkbox" name="selected[]" value="<?php echo $user_group['user_group_id']; ?>" checked="checked" />
                        <?php } else { ?>
                        <input type="checkbox" name="selected[]" value="<?php echo $user_group['user_group_id']; ?>" />
                        <?php } ?></td>
                        <td class="text-left"><?php echo $user_group['name']; ?></td>
                        <td class="text-right"><a href="<?php echo $user_group['edit']; ?>" class="btn btn-primary"><i class="fa fa-pencil"></i></a></td>
                    </tr>
                    <?php } ?>
                    <?php } else { ?>
                    <tr>
                        <td class="text-center" colspan="3">No Results</td>
                    </tr>
                    <?php } ?>
                </tbody>    
                </table>
                </div>
            </div>
        </div>
    </div>

    <?php echo form_close();?>
</div>
<?php echo Modules::run('admin/common/footer/index');?>

1 个答案:

答案 0 :(得分:0)

我已经解决了我的问题我必须将主视图重新加载到删除表单验证虚假区域,即$ this-&gt; index();

由于某些原因,当提交表单时,错误重定向正在清除错误。 Al现在修复了

<?php

class User_group_list extends Admin_Controller {

public function index() {
    $data['title'] = ucwords(str_replace('_', ' ', $this->router->fetch_class()));

    $data['breadcrumbs'] = array();

    $data['breadcrumbs'][] = array(
        'text' => 'Home',
        'href' => site_url('admin/dashboard')
    );

    $data['breadcrumbs'][] = array(
        'text' =>  ucwords(str_replace('_', ' ', $this->router->fetch_class())),
        'href' => base_url('admin/user_group')
    );

    $data['user_groups'] = array();

    $results = $this->get_user_groups();

    foreach ($results as $result) {
        $data['user_groups'][] = array(
            'user_group_id' => $result['user_group_id'],
            'name' => $result['name'],
            'edit' => site_url('admin/user_group/update' .'/'.$result['user_group_id'])
        );
    }

    if (isset($_POST['selected'])) {
        $data['selected'] = (array)$_POST['selected'];
    } else {
        $data['selected'] = array();
    }

    $this->load->view('template/user_group/user_group_list', $data);
}

public function get_user_groups() {
    $user_group = $this->db->get($this->db->dbprefix . 'user_group');
    if ($user_group->num_rows()) {
        return $user_group->result_array();
    } else {
        return false;
    }
}

public function delete() {
    $this->load->library('form_validation');

    $selected_post = $this->input->post('selected');

    $this->form_validation->set_rules('selected[]', 'selected', 'required|callback_validateDelete');

    if ($this->form_validation->run($this) == FALSE) {
        $this->index();
    } else {
        $this->session->set_flashdata('success', 'Success: You have modified user groups!');
        redirect('admin/user_group');
    }
}

public function delete_user_group($user_group_id) {
    $this->db->where('user_group_id', $user_group_id);
    $this->db->delete($this->db->dbprefix . 'user_group');
}

public function validateDelete() {
    $this->load->library('user');
    $this->load->library('form_validation');

    if ($this->user->hasPermission('modify', "User_group_list")) {
        return true;
    } else {
        $this->form_validation->set_message('validateDelete', 'Warning: You do not have permission to modify user groups');
        return false;
    }

}

}