Join the Stack Overflow Community
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I have a form with an array field that allows the user to select multiple category ids. They must select at least one category but can select more than one. My form validation needs to ensure at least one category id is specified and then for each category id it needs to check that it is a valid category. Here is what I have:

$this->form_validation->set_rules('event_categories', 'Categories', 'required');
$this->form_validation->set_rules('event_categories[]', 'Categories', 'integer|exists[category.id]');

I have extended the form validation library and added the exists method which looks like this:

/**
 * Checks to see if a value exists in database table field
 *
 * @access  public
 * @param   string
 * @param   field
 * @return  bool
 */
public function exists($str, $field)
{
    //die("fe");
    list($table, $field)=explode('.', $field);
    $query = $this->CI->db->limit(1)->get_where($table, array($field => $str));

    if($query->num_rows() !== 0) {
        return TRUE;
    }
    else {
        if(!array_key_exists('exists',$this->_error_messages)) {
            $this->CI->form_validation->set_message('exists', "The %s value does not exist");
        }
        return FALSE;
    }
}

The problem is even when I submit a valid array of category ids the form validation fails on the required check and says that I must submit some even though I have.

share|improve this question
up vote 2 down vote accepted

From CI DOCS https://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#arraysasfields

$this->form_validation->set_rules('event_categories', 'Categories', 'required');

Should be

$this->form_validation->set_rules('event_categories[]', 'Categories', 'required');

To show form error Use

echo form_error('event_categories[]');
share|improve this answer
    
I've done that but if I submit the form without selecting any categories the validation should fail but it doesn't – geoffs3310 Feb 10 at 13:25
    
Ohkk then check your html if its first option should do not have any value e.g. <option value="" selected>select me</option> mark that no space in value :) – Praveen Kumar Feb 10 at 13:37
    
@geoffs3310 also you can try $this->form_validation->set_rules('event_categories[]', 'Categories', 'trim|required'); – Praveen Kumar Feb 10 at 13:37
    
Ahh I've sorted it now, it's because I was using form_error('event_categories') instead of form_error('event_categories[]') – geoffs3310 Feb 10 at 13:45
    
Edited as answer suggested by you :) – Praveen Kumar Feb 10 at 13:48

Solved this now, it was because I was calling form_error('event_categories') instead of form_error('event_categories[]')

So just to clarify if you are submitting an array that is required the correct validation rule format is:

$this->form_validation->set_rules('event_categories[]', 'Categories', 'required');
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.