1
votes

I'm using codeigniter and trying to validate my form using custom callback check function. But it is really confuse to me because the validation always return FALSE.

Here is the form_input:

echo form_input(array(
       'id' => 'aff_hos_0', 
       'name' => 'aff_hos[]', 
       'value' => '',
       'class' => 'form-control auto_form',
       'placeholder' => 'Masukkan Nama Rumah Sakit',
       'style' => 'width:100%'));

Here is the data POST by form:

Array
(
    [0] => RS Pantai Indah Kapuk, Cengkareng, Jakarta, Jakarta
    [1] => RS Siloam Kebonn Jeruk, Kb. Jeruk, Jakarta, Jakarta
)

This is when I set the form_validation:

$this->form_validation->set_rules('aff_hos[]', 'Afiliasi Rumah Sakit ', 'callback_check_chosen_filled[Afiliasi Rumah Sakit]');

And this is the callback_check function, what I'm trying to achieve with this callback is to check if the array of input (aff_hos[]) not blank:

function check_chosen_filled($post_array, $field) {
    if (!isset($post_array) || (!is_array($post_array) && !count($post_array) > 0)) {
        $this->form_validation->set_message('check_chosen_filled', $field . ' wajib diisi'.print_r($post_array));
        return FALSE;
    } else {
        foreach ($post_array as $row):
            if (strlen($row) == 0) {
                $this->form_validation->set_message('check_chosen_filled', $field . ' wajib diisi');
                return FALSE;
            }
        endforeach;
    }
    return TRUE;
}
1

1 Answers

0
votes

The problem is that you take first parameter as array in your callback. Actually, it is not array. CI validation library iterate each array value to call custom callback. That's why your code should be like that.

function check_chosen_filled($value, $field) {
    if (empty($value)) {
        // set validation error msg
        return FALSE;
    }
    return TRUE;
}

Hope it will be useful for you.