質問

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]');
役に立ちましたか?

解決

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

他のヒント

No there isn't a validation rule for that specific case.

But you can create your own validation rules: Look here (Codeingiter UserGuide)

I.E.:

$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 in_list codeigniter form validation rule.

$this->form_validation->set_rules('drink', 'Drink', 'in_list[beer,soda,wine,water]');
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top