CodeIgniter has many validation rules but is there any rule for checking if the value from a certain field is present in an array(given as parameter to the validation rule)?
For example:
$possible_values = array('beer', 'soda', 'wine', 'water');
$this->form_validation->set_rules('drink', 'Drink', 'required|trim|found_in_array[possible_values]');
No there isn't a validation rule for that specific case.
But you can create your own validation rules: Look here (Codeingiter UserGuide)
IE:
$this->form_validation->set_rules('username', 'Username', 'callback_is_inArray[someValues]');
public function is_inArray($str, $values) {
return in_array($str, $values);
}
you can use callback_function_name
like so,
$this->form_validation->set_rules('drink', 'Drink', 'callback_customInArray');
public function customInArray($str)
{
$possible_values = array('beer', 'soda', 'wine', 'water');
if(in_array($str, $possible_values){return true;}
return false;
}
read more about that in CI Form Validation
您可以使用in_list
codeigniter表单验证规则。
$this->form_validation->set_rules('drink', 'Drink', 'in_list[beer,soda,wine,water]');
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.