【问题标题】:Codeigniter-HMVC form validation checkbox array callback validation not workCodeigniter-HMVC 表单验证复选框数组回调验证不起作用
【发布时间】:2015-07-07 13:29:53
【问题描述】:

Codeigniter 表单验证复选框验证不起作用。我一直在做一个项目,我可以在我的视图上选择不同的复选框。当我提交表单时如果所有框都是空的,那么应该抛出回调消息

但由于某种原因无法使用

<input type="checkbox" name="tables[]" value="<?php echo $table;?>" />

我查看了 stackoverflow 网站 和其他网站并尝试了很多但没有一些工作。

问题让我的回调函数工作的最佳解决方案是什么,如果所有复选框都为空,那么将抛出我的表单验证消息?

对于任何想要 var dump 结果的人

array(3) { 
    [0]=> string(4) "user" 
    [1]=> string(10) "user_group" 
    [2]=> string(16) "user_join_status" 
} 

控制器

<?php

class Backup extends MX_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->library('admin/users');
        $this->load->model('admin/tool/model_tool_backup');
    }

    public function index() {

        $data['tables'] = $this->model_tool_backup->get_tables();

        $this->load->library('form_validation');

        $this->form_validation->set_rules('tables[]', 'Table', 'callback_check');

        // $this from library MY_Form_Validation for HMVC

        if ($this->form_validation->run($this) == FALSE) {

            $data['sidebar'] = Modules::run('admin/common/sidebar/index');
            $data['navbar'] = Modules::run('admin/common/navbar/index');
            $data['header'] = Modules::run('admin/common/header/index');
            $data['footer'] = Modules::run('admin/common/footer/index');

            $this->load->view('tool/backup_view', $data);

        } else {

            var_dump($this->input->post('tables[]'));

            //$this->output->set_header('Pragma: public');
            //$this->output->set_header('Expires: 0');
            //$this->output->set_header('Content-Description: File Transfer');
            //$this->output->set_header('Content-Type: application/octet-stream');
            //$this->output->set_header('Content-Disposition: attachment; filename=' . $this->db->database . '_' . date('Y-m-d_H-i-s', time()) . '_backup.sql');
            //$this->output->set_header('Content-Transfer-Encoding: binary');

            //$this->output->append_output($this->backup_codeigniter($this->input->post('backup[]')));

        }
    }

    public function check($post_tables) {
        $post_tables = $this->input->post('tables[]');

        if (!isset($post_tables)) {
            $this->form_validation->set_message('check', 'You Must Select At Least One Table To Do Back Up!');
            return FALSE;
        } else {
            return TRUE;
        }
    }

    public function backup_codeigniter($tables) {
        $this->load->dbutil();

        $prefs = array(
            'tables' => $tables, 
            'ignore' => array(),
            'format' => 'txt',
            'filename' => $this->db->database . '_' . date('Y-m-d_H-i-s', time()) . '_backup.sql',
            'add_drop' => TRUE,
            'add_insert' => TRUE,
            'newline' => "\n" 
        );

        return $this->dbutil->backup($prefs);
    }
}

查看

<?php echo form_open_multipart('admin/tool/backup', array('class' => 'form-horizontal')); ?>
<?php echo validation_errors('<div class="alert alert-danger"><i class="fa fa-exclamation-circle"></i> <button type="button" class="close" data-dismiss="alert">&times;</button>', '</div>'); ?>
<div class="form-group">
    <label class="col-sm-2 control-label">Backup</label>
    <div class="col-sm-10">
        <div class="well well-sm" style="height: 150px; overflow: auto;">
            <?php foreach ($tables as $table) { ?>
                <div class="checkbox">
                    <label>
                        <input type="checkbox" name="tables[]" value="<?php echo $table; ?>"/>
                        <?php echo $table; ?></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="button-group">
    <div class="form-group text-right">
        <button type="submit" class="btn btn-lg btn-inverse">Click to Backup Database</button>
    </div>
</div>
<?php echo form_close(); ?>

HMVC 的 MY_Form_Validation 库那为什么需要在 $this-&gt;form_validation-&gt;run($this) 中使用 $this 进行回调

<?php

class MY_Form_validation extends CI_Form_validation {

    function run($module = '', $group = '') {
        (is_object($module)) AND $this->CI = &$module;
        return parent::run($group);
    }

}   

【问题讨论】:

  • 在您的check() 函数中,您应该按名称而不是数组传递 post 变量,如下所示:$this-&gt;input-&gt;post('tables');
  • 我试过了,但如果所有复选框都未选中仍然不会抛出错误。
  • 在您的验证行中怎么样:$this-&gt;form_validation-&gt;set_rules('tables', 'Table', 'callback_check');
  • 实际上,您的chec() 函数应该接收一个参数。像这样check($tables) 这会将表格信息发送到回调函数,而无需运行$this-&gt;input-&gt;post(),然后执行您的条件。试试看,然后告诉我。
  • 选中所有复选框后,var_dump($this-&gt;input-&gt;post('tables[]')); 会得到什么?

标签: codeigniter codeigniter-hmvc


【解决方案1】:

更改这些行(相应地):

$this->form_validation->set_rules('tables[]', 'Table', 'callback_check');
...
if ($this->form_validation->run($this) == FALSE) {

到这里:

$this->form_validation->set_rules('tables', 'Table', 'callback_check');
...
if ($this->form_validation->run() == FALSE) {

现在你的回调函数应该接收这样发送的参数:

public function check($post_tables) {

    if (!isset($post_tables)) {
        $this->form_validation->set_message('check', 'You Must Select At Least One Table To Do Back Up!');
        return FALSE;
    } else {
        return TRUE;
    }

}

更新:

由于这个问题与 Codeigniter 的非标准 HMVC 设置有关,阅读这篇博客,我发现了这一行:

 $this->form_validation->CI =& $this;   // Hack to make it work properly with HMVC

你可能会找到你要找的东西:https://github.com/ci-bonfire/Bonfire/issues/295

【讨论】:

  • 没有快乐会继续尝试。
  • 我更新了我的答案。 $this-&gt;form_validation-&gt;run($this) $this 不应发送。
  • 我需要 $this 变量来运行我的 HMVC 回调 MY_Form_validation,因为它不会在其他方面起作用。如果使用普通文本输入,回调函数与 $this 一起工作正常,但复选框数组有问题。
  • 啊.. 很好的 HMVC.. 我不太熟悉该设置,因为它不是标准的,我会将该标签添加到您的 OP 中。
  • 正如我所说,如果只使用普通的文本帖子和密码帖子,回调工作正常,但 codeigniter 在回调中的复选框数组有问题。
【解决方案2】:

问题已解决 似乎是 codeigniter hmvc 表单验证无法验证复选框数组的问题。所有其他回调都可以正常工作,尽管只是选中的框没有验证正确。

我的修复工作

因此,我不得不使用服务器 REQUEST_METHOD 使用自己的自定义验证,如下例所示。

<?php

class Backup extends MX_Controller {

private $error = array();

public function __construct() {
    parent::__construct();
    $this->load->library('admin/users');
    $this->load->model('admin/tool/model_tool_backup');
    $this->load->library('form_validation');
}

public function index() {
    $tables = $this->input->post('tables');

    $data['tables'] = $this->model_tool_backup->get_tables();

    if (($this->input->server('REQUEST_METHOD') == 'POST') && $this->validate()) {

    }

    if (isset($this->error['warning'])) {
        $data['error_warning'] = $this->error['warning'];
    } else {
        $data['error_warning'] = '';
    }

    if (isset($this->error['tables'])) {
        $data['error_tables'] = $this->error['tables'];
    } else {
        $data['error_tables'] = '';
    }

    $data['sidebar'] = Modules::run('admin/common/sidebar/index');
    $data['navbar'] = Modules::run('admin/common/navbar/index');
    $data['header'] = Modules::run('admin/common/header/index');
    $data['footer'] = Modules::run('admin/common/footer/index');

    $this->load->view('tool/backup_view', $data);

}

public function validate() {
    if (!isset($_POST['tables'])) {
        $this->error['tables'] = 'You must select at least one item';
    }

    return !$this->error;
}

public function backup_codeigniter($tables) {
    $this->load->dbutil();

    $prefs = array(
        'tables' => $tables, 
        'ignore' => array(),
        'format' => 'txt',
        'filename' => $this->db->database . '_' . date('Y-m-d_H-i-s', time()) . '_backup.sql',
        'add_drop' => TRUE,
        'add_insert' => TRUE,
        'newline' => "\n" 
    );

    return $this->dbutil->backup($prefs);
}

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-29
    • 2010-11-24
    • 2014-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-24
    • 1970-01-01
    相关资源
    最近更新 更多