1

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]');

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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);
}

Comments

1

You can use in_list codeigniter form validation rule.

$this->form_validation->set_rules('drink', 'Drink', 'in_list[beer,soda,wine,water]');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.