CodeIgniter callback functions with parameters

2019-04-02 01:47发布

问题:

(Edit: I've since realised that perhaps it's not possible to pass arrays as parameters in callbacks?)

I'm trying to work out how to pass parameters to a callback function in CI. I've read the documentation but there isn't much on the subject except:

To invoke a callback just put the function name in a rule, with "callback_" as the rule prefix. If you need to receive an extra parameter in your callback function, just add it normally after the function name between square brackets, as in: "callback_foo[bar]", then it will be passed as the second argument of your callback function.

What I'm trying to do is create a callback which checks if an has been selected which shouldn't have been. So if someone selects the option which says "Please select" it won't be added to the database. Task types is just a table with primary keys and a name field and about 10 rows.

Controller So here is my controller code (cut down):

function Add_Task()
{
  $task_types_get = $this->task_model->Get_Task_Types();//Get available task types.
  $this->options->task_types = $task_types_get->result();
  $not_selectable = array(1);//Non selectable values for select. Added to callback function below for validation. These are pks.

  $this->form_validation->set_rules("task_type","Task Types","required|callback__Not_Selectable[$not_selectable]");

  if($this->form_validation->run())
  {
     //Add to db etc..
  }
}

Callback And my callback to check if something is not selectable:

function _Not_Selectable($option,$values=array())
{  
  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;
}

View The data which is coming back from the model is ok, but there are not validation errors. My view is as follows:

<? $this->load->view('includes/header'); ?>

  <?=form_open();?>

  <div class="form_row">
    <div class="form_field">
      <label for="task_desc">Task Type</label>
    </div>

    <div class="form_field name_element" id="name-list">
      <select name="task_type" id="task_select">
        <? foreach($task_types as $t => $v):
             echo "<option ".set_select('task_type', $v->tt_id)." value=\"{$v->tt_id}\">{$v->name}</option>\n";
           endforeach;
        ?>
      </select>
      <?=form_error('task_type');?>
    </div>
  </div>

  <?=form_submit('add_task', 'Add Task');?>

  <?=form_close();?>

<? $this->load->view('includes/footer'); ?>

Errors The error I'm getting is:

A PHP Error was encountered
Severity: Warning
Message: in_array() [function.in-array]: Wrong datatype for second argument
Filename: controllers/tasks.php
Line Number: 112 (NOTE: this is the in_array line in the callback function.)

The error suggests that the information passed is not an array but I've even defined array as default. I did a print_r() on the $options array in the callback function but it just printed out an empty Array.

Thanks.

回答1:

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


回答2:

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
}