Codeigniter-HMVC form validation checkbox array callback validation not work -


codeigniter form validation check box validation not work. have been working on project can select different check boxes on view. , when submit form if boxes empty should throw callback message.

but reason not work

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

i have looked around stack overflow website , other , tried lots non work.

question best solution callback function working if check boxes empty throw form validation message?

for 1 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 library my_form_validation 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 @ least 1 table 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 $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 backup database</button>     </div> </div> <?php echo form_close(); ?> 

my_form_validation library hmvc why need $this in $this->form_validation->run($this) callbacks

<?php  class my_form_validation extends ci_form_validation {      function run($module = '', $group = '') {         (is_object($module)) , $this->ci = &$module;         return parent::run($group);     }  }    

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 callback function should receive parameter sent this:

public function check($post_tables) {      if (!isset($post_tables)) {         $this->form_validation->set_message('check', 'you must select @ least 1 table up!');         return false;     } else {         return true;     }  } 

update:

since problem relates non-standard hmvc setup codeigniter, read through blog, found line:

 $this->form_validation->ci =& $this;   // hack make work hmvc 

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


Comments

Popular posts from this blog

android - Pass an Serializable object in AIDL -

How to provide Authorization & Authentication using Asp.net, C#? -

How to use Authorization & Authentication in Asp.net, C#? -