CodeIgniter Forums
Select multiple validation problem... very strange... - Printable Version

+- CodeIgniter Forums (
+-- Forum: Using CodeIgniter (
+--- Forum: General Help (
+--- Thread: Select multiple validation problem... very strange... (/showthread.php?tid=68190)

Select multiple validation problem... very strange... - gbwebapps - 06-07-2017

Hi there to everyone...

I have a strange problem when I try to validate a select multiple field in a form contained in a bootstrap's modal.
I don't think the problem is due to the modal, but anyway, I go to explain...

The mechanism is developed with ajax technique.

I have a form in which I insert products. I have 4 fields: product, brand, categories and description. (pic 1)

I use to put the rules validation inside the model in this way:

public function rules()
$rules = array(
'product' => array(
'field' => 'product',
'label' => 'lang:products_label_product',
'rules' => 'trim|required'
'brand_id' => array(
'field' => 'brand_id',
'label' => 'lang:products_label_brand',
'rules' => 'trim|required'
'categories' => array(
'field' => 'categories[]',
'label' => 'lang:products_label_categories',
'rules' => 'trim|required'
'description' => array(
'field' => 'description',
'label' => 'lang:products_label_description',
'rules' => 'trim|required'
return $rules;

I call rules from inside the controller:

$rules = $this->Products_model->rules();

These are the fields:

$output .= '<div class="form-group">';
 $output .= '<label>' . lang('products_label_product') . '</label>';
 $output .= '<input type="text" class="form-control" name="product">';
 $output .= '<span class="text_error" id="product"></span>';
$output .= '</div>';
$output .= '<div class="form-group">';
$output .= '<label>' . lang('products_label_brand') . '</label>';
$output .= '<select name="brand_id" class="form-control">';
$output .= '<option value="">'. lang('products_label_choice_brand_id') .'</option>';
$output .= '<option value="apple">apple</option>';
$output .= '<option value="samsung">samsung</option>';
$output .= '<option value="hewlett packard">hewlett packard</option>';
$output .= '</select>';
$output .= '<span class="text_error" id="brand_id"></span>';
$output .= '</div>';
$output .= '<div class="form-group">';
$output .= '<label>' . lang('products_label_categories') . '</label>';
$output .= '<select name="categories[]" class="form-control" multiple>';
$output .= '<option value="">'. lang('products_label_choice_categories') .'</option>';
$output .= '<option value="hardware">hardware</option>';
$output .= '<option value="printers">printers</option>';
$output .= '<option value="scanners">scanners</option>';
$output .= '</select>';
$output .= '<span class="text_error" id="categories[]"></span>';
$output .= '</div>';
$output .= '<div class="form-group">';
 $output .= '<label>' . lang('products_label_description') . '</label>';
 $output .= '<textarea class="form-control" name="description" cols="30" rows="10"></textarea>';
 $output .= '<span class="text_error" id="description"></span>';
$output .= '</div>';

If there are some errors, I send through json to ajax the errors in this way, immediately above the call to the rules:

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

    foreach($_POST as $k => $v):
         $this->data['errors'][$k] = form_error($k);

    $this->data['messages'] = lang('msg_form_validation_error');

    echo json_encode($this->data);


In the view I have this code that receive the errors:


var data = JSON.parse(data);
         if(data.errors) {
           $.each(data.errors, function(key, value) {
               var element = $('span#' + key);
         } else {


All this mechanism is working fine, except for the multiple field, I can't display the error....
The strange thing is that if I become the multiple select in simple select (so not an array) and I remove all brackets ([]),
the field display properly the error...

Someone did have a similar problem which he could solve?

CI 3.1.4

Thank you in advance