In AJAX requests, how can we validate POST arrays that are received? I am using form_validation library of the CodeIgniter framework for validation purposes. I am able to validate strings, integers and numeric values without problems. But when it comes to integer arrays, it doesn't help.
Following is my code:
$this->form_validation->set_rules('order_amount_paid', 'Amount Paid', 'trim|numeric');//This is ok and working
$this->form_validation->set_rules('product_categoryid_array[]', 'Product categories', 'trim|required|callback_validate_product_info');
'product_categoryid_array' is actually an array that is posted via ajax request. The callback function that I have used for validating this is as follows:
public function validate_product_info($array){
if(count($array) <= 0){
return FALSE;
}
elseif(count($array) > 0){
$total_count = count($array);
for($i = 0; $i < $total_count; $i++){
if(($array[$i] != '' && !is_numeric($array[$i])) OR $array[$i] <= 0){
return FALSE;
}
}
return TRUE;
}
else{
return TRUE;
}
}
In the callback function, I think I am not getting 'product_categoryid_array' as an array, but in fact, as a string.
In some cases, I get an error 'Uninitialized string offset'. How can I validate post arrays successfully.