0

Codeigniter form validation check box validation not work. I have been working on a project where I can select different check boxes on my view. And when I submit my form if all boxes are empty then should throw callback message.

But for some reason will not work with

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

I have looked around stack overflow website and other and tried lots but non some to work.

Question What would be the best solution to get my callback function working so if all check boxes are empty then will throw my form validation message?

For any one who wants var dump results

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

Controller

<?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);
    }
}

View

<?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(); ?>

MY_Form_Validation Library For HMVC That why need $this in $this->form_validation->run($this) for callbacks

<?php

class MY_Form_validation extends CI_Form_validation {

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

}   
  • in your `check()` function, you should pass the post variable by name not by array, like this: `$this->input->post('tables');` – CodeGodie Jul 07 '15 at 14:23
  • I tried that but still if all checked boxes un-checked still will not throw error. –  Jul 07 '15 at 14:25
  • how about in your validation line: `$this->form_validation->set_rules('tables', 'Table', 'callback_check');` ? – CodeGodie Jul 07 '15 at 14:29
  • actually, your `chec()` function should receive a parameter. like this `check($tables)` this will send the table info to the callback function without you having to run `$this->input->post()`, then perform your conditions. Try that and let me know. – CodeGodie Jul 07 '15 at 14:32
  • What do you get with `var_dump($this->input->post('tables[]'));` when all boxes are checked? – Tpojka Jul 07 '15 at 15:26
  • @Tpojka Same results –  Jul 07 '15 at 15:32
  • How PHP determines what value passed? If select two checkboxes there is an array of two elements? – Tpojka Jul 07 '15 at 15:37
  • show a var_dump of the passed $post_variables from the check function before you do `$post_tables = $this->input->post('tables[]');` – Atural Jul 07 '15 at 21:52
  • @sintakonte I all ready did please look at top same result for `$this->input->post('tables');` and `$this->input->post('tables[]');` –  Jul 07 '15 at 23:48
  • ahm as far as i can see your var dump is in the else section in your index controller - but anyway if you say its from the check function - you know at least the validation function calls the check function - isn't it ? In this case - whats the return var_dump from `$this->form_validation->run($this)` – Atural Jul 08 '15 at 07:34

2 Answers2

1

change these lines (accordingly):

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

to this:

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

Now your callback function should receive the parameter sent like this:

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;
    }

}

UPDATE:

Since this problem relates to the non-standard HMVC setup for Codeigniter, read through this blog, I found this line:

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

you might find what you are looking for: https://github.com/ci-bonfire/Bonfire/issues/295

CodeGodie
  • 12,116
  • 6
  • 37
  • 66
  • No joy will keep trying. –  Jul 07 '15 at 14:37
  • i updated my answer. In `$this->form_validation->run($this)` `$this` should not be sent. – CodeGodie Jul 07 '15 at 14:41
  • I need $this variable in run for my HMVC callbacks MY_Form_validation because it will not work other wise. callback function works fine with $this if use normal text input but has trouble with checked box array. –  Jul 07 '15 at 14:44
  • ahh.. good ol HMVC.. im not too familiar with that setup since it is not standard, i will add that tag to your OP. – CodeGodie Jul 07 '15 at 14:45
  • As i said call backs work fine if just use normal text post and password post but has codeigniter has trouble with checked box array in callbacks. –  Jul 07 '15 at 14:52
0

Problem Solved Seems to be a issue with codeigniter hmvc form validation not working with validating checked box arrays. All other call backs work fine though just checked boxes not validating correct.

My Fix Working

So there for I have had to use own custom validation using server REQUEST_METHOD like in example below.

<?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);
}

}