• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to handle form validation with bulk editing?

While in the midst of converting my existing application to the CI framework, I've run into another stumbling block. One of the modules in my application allows me to bulk edit items. Here is my view:

foreach($cash_flow as $key => $value) {
<td>&lt;?php echo $j; ?&gt;
<td>&lt;input type="checkbox" name="delete[&lt;?php echo $value-&gt;id; ?&gt;]" value="1" /&gt;&lt;/td>
<td>&lt;?php echo $value->cat_desc; ?&gt;</td>
<td>&lt;input type="text" name="historical[&lt;?php echo $value-&gt;id; ?&gt;]" value="&lt;?php echo set_value('historical['. $value-&gt;id .']', $value-&gt;historical); ?&gt;" /&gt;&lt;/td>
<td>&lt;input type="text" name="projected[&lt;?php echo $value-&gt;id; ?&gt;]" value="&lt;?php echo set_value('projected['. $value-&gt;id .']', $value-&gt;projected); ?&gt;" /&gt;&lt;/td>
<td>&lt;input type="text" name="adjusted[&lt;?php echo $value-&gt;id; ?&gt;]" value="&lt;?php echo set_value('adjusted['. $value-&gt;id .']', $value-&gt;adjusted); ?&gt;" /&gt;&lt;/td>
echo form_hidden('cash_flow_category_id['. $value->id .']', $value->cash_flow_category_id);

The value contained in $value->id is the unique id of the record being edited.

How would I use this in conjunction with $this->form_validation->set_rules? How can I set messages for which record/row need to be fixed? I tried this:

$arr = $this->input->post('cash_flow_category_id');

foreach($arr as $key) {
$this->form_validation->set_rules('cash_flow_category_id['. $key .']', 'Cash Flow Category', 'required|clean_int|cash_flow_check_values');
$this->form_validation->set_rules('historical['. $key .']', 'Historical Value', 'default_zero|clean_dec');
$this->form_validation->set_rules('projected['. $key .']', 'Projected Value', 'default_zero|clean_dec');
$this->form_validation->set_rules('adjusted['. $key .']', 'Adjusted Value', 'default_zero|clean_dec|greater_than[11000]');

But I don't get any errors returned for the last rule (greater than 11,000) when I have several rows with values below that. I appreciate any help. Thanks!

I found my mistake. When I ran my foreach, I need to use $key => $value instead of just $key--I didn't realize I did that.

$arr = $this->input->post('cash_flow_category_id');

foreach($arr as $key => $value) {
$this->form_validation->set_rules('cash_flow_category_id['. $key .']', 'Cash Flow Category', 'required|clean_int|cash_flow_check_values');
$this->form_validation->set_rules('historical['. $key .']', 'Historical Value', 'default_zero|clean_dec');
$this->form_validation->set_rules('projected['. $key .']', 'Projected Value', 'default_zero|clean_dec');
$this->form_validation->set_rules('adjusted['. $key .']', 'Adjusted Value', 'default_zero|clean_dec');

My other issue is I currently use a function, in this case "cash_flow_check_values", which checks the values of several fields. This is the current function:

public function cash_flow_check_values() {
if($this->ci->input->post('historical') == 0 and $this->ci->input->post('projected') == 0 and $this->ci->input->post('adjusted') == 0) {
  return false;
} else {
  return true;

How would I be able to do this through a bulk edit? I would have to know what key in the array I was on to set the appropriate field name. Any ideas? Thanks!

You can pass additional parameters to your callback function.

If your rule is set like:
//notice parameter passed in brackets
$this->form_validation->set_rules('field', 'field name', 'cash_flow_check_values[6]');

And your validation rules was like:
public function cash_flow_check_values($field_value, $parameters)
  echo $field_value; //value of whatever field this rule was run on
  echo $parameters; // will be "6", and it will always be a string

This isn't documented in the user guide afaik, but if you look at the validation class for the validation rules they created that allow parameters (like min_length, etc), that's how they are doing it. So you could pass your array key here when you are "bulk" setting your rules.

Edit: actually the user guide does mention it for the validation callbacks:
Quote: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.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.