[eluser]bkirkman[/eluser]
To expound on the revision above, the following is the way I have set up the controller and the view. Set up validation in the controller as follows.
Controller:
Code:
...
$this->form_validation->set_rules('item_id[]', 'Item Id', 'trim');
$this->form_validation->set_rules('item_no[]', 'Item No.', 'trim');
$this->form_validation->set_rules('item_description[]', 'Item Description', 'trim');
$this->form_validation->set_rules('item_type[]', 'Item Type', 'trim');
...
item_id is the auto-incremented key value for items in a database and is hidden in the view. The other three fields are columns in the table and will be fields in the view.
Then in the view, I chose to dynamically set the field values depending on the existing condition. I suppose there's a more elegant way to refactor this code, but I, too, spent a lot of time pulling my hair out. I was just happy with a solution.
View:
Code:
...
<table id="tblItems">
<tr>
<td class="txtBase">Item No.</td>
<td class="txtBase">Item Description</td>
<td class="txtBase">Item Type</td>
</tr>
<?php if(isset($_POST['item_no'])) : ?> <!-- this is to set values for forms that do not pass validation -->
<?php foreach($_POST['item_no'] as $row) : ?>
<tr>
<input type="hidden" name="item_id[]" value="<?php echo set_value('item_id[]'); ?>" />
<td><input type="text" size="10" name="item_no[]" value="<?php echo set_value('item_no[]'); ?>" /></td>
<td><input type="text" size="40" name="item_description[]" value="<?php echo set_value('item_description[]'); ?>" /></td>
<td><input type="text" size="10" name="item_type[]" value="<?php echo set_value('item_type[]'); ?>" /></td>
</tr>
<?php endforeach; ?>
<?php elseif(isset($item_no)) : ?> <!-- this is to set values for editing forms that have existing item data -->
<?php $i = 0; ?>
<?php foreach($item_no as $row) : ?>
<tr>
<input type="hidden" name="item_id[]" value="<?php echo set_value('item_id[]', $item_id[$i]); ?>" />
<td><input type="text" size="10" name="item_no[]" value="<?php echo set_value('item_no[]', $item_no[$i]); ?>" /></td>
<td><input type="text" size="40" name="item_description[]" value="<?php echo set_value('item_description[]', $item_description[$i]); ?>" /></td>
<td><input type="text" size="10" name="item_type[]" value="<?php echo set_value('item_type[]', $item_type[$i]); ?>" /></td>
</tr>
<?php $i++; ?>
<?php endforeach; ?>
<?php else : ?> <!-- this is to create new form fields if editing/adding a form with no items -->
<tr>
<input type="hidden" name="item_id[]" value="<?php echo set_value('item_id[]'); ?>" />
<td><input type="text" size="10" name="item_no[]" value="<?php echo set_value('item_no[]'); ?>" /></td>
<td><input type="text" size="40" name="item_description[]" value="<?php echo set_value('item_description[]'); ?>" /></td>
<td><input type="text" size="10" name="item_type[]" value="<?php echo set_value('item_type[]'); ?>" /></td>
</tr>
<tr>
<input type="hidden" name="item_id[]" value="<?php echo set_value('item_id[]'); ?>" />
<td><input type="text" size="10" name="item_no[]" value="<?php echo set_value('item_no[]'); ?>" /></td>
<td><input type="text" size="40" name="item_description[]" value="<?php echo set_value('item_description[]'); ?>" /></td>
<td><input type="text" size="10" name="item_type[]" value="<?php echo set_value('item_type[]'); ?>" /></td>
</tr>
<?php endif; ?>
</table>
...
I'm not sure exactly how others are implementing this "feature." I'm using javascript to add blank fields to the form if the user needs to input more data. That's a discussion for another time. Also, writing the post data back to the database is not exactly trivial when dealing with edits and deletions. Perhaps that will also be a post for another time. Maybe a wiki is in order for forms such as this.