• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Select multiple validation problem... very strange...

#1
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:

Code:
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:

Code:
$rules = $this->Products_model->rules();
$this->form_validation->set_rules($rules);

These are the fields:

Code:
$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:

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

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

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

    echo json_encode($this->data);

ecc.....

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

Code:
...........

var data = JSON.parse(data);
         if(data.errors) {
           $.each(data.errors, function(key, value) {
               var element = $('span#' + key);
               element.html(value);
           })
           $('#messages').html(data.messages).fadeIn(100).delay(2000).fadeOut(100);
         } 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

Giorgio


Attached Files Thumbnail(s)
   
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.