CodeIgniter callback functions with parameters
The problem is here:
"required|callback__Not_Selectable[$not_selectable]"
This translates to the string:
"required|callback__Not_Selectable[Array]"
This is what happens when you treat arrays as strings in PHP.
This problem is a limitation of Codeigniter's form validation library, there is no proper way to use arrays in callbacks or validation rules, you'll have to use strings. Try this:
$not_selectable = implode('|', $your_array);
This will make something like 1|4|18|33
. Then set your rules as you are currently doing, but in your callback be prepared for a pipe delimited string rather than an array, and use explode()
to create one:
function _Not_Selectable($option, $values_str = ''){ // Make an array $values = explode('|', $values_str); if(in_array($option,$values))//Is the value invalid? { $this->form_validation->set_message('_Not_Selectable', 'That option is not selectable.'); return false; } return true;}
You should extend the CI_Form_validation lib rather than trying to use callbacks.
To check if a selectbox is valid simple add default to its value then check for that.
<select><option value="default">Select one</option></select>
MY_Form_Validation
|->
public function check_select_values($option){ if($option === 'default') // try again slim jim}