[eluser]jasonswett[/eluser]
Here's what I ended up doing.
Validation controller:
Code:
<?php
class Contact_update extends Controller {
function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('validation');
// Establish validation rules.
$rules['first_name'] = 'required';
$rules['last_name'] = 'required';
$rules['address'] = 'required';
$rules['city'] = 'required';
$rules['state_id'] = 'required';
$rules['zip'] = 'required';
// Set rules.
$this->validation->set_rules($rules);
// Establish field names.
$fields['first_name'] = 'First Name';
$fields['last_name'] = 'Last Name';
$fields['address'] = 'Address';
$fields['city'] = 'City';
$fields['state_id'] = 'State';
$fields['zip'] = 'Zip';
$fields['id'] = 'id';
// Set field names.
$this->validation->set_fields($fields);
// If validation fails, go back to the edit page. If success, go to the list page (for now).
if ($this->validation->run() == FALSE)
{
// Assume for now that we're dealing with the edit page as opposed to add.
$data['title'] = 'Edit Contact';
// Get the data for the state select field.
$data['states'] = $this->db->get('state');
// Put the user's values into the $data object.
$data['contact'] = $this->validation;
// Load the template.
$this->load->view('contact/edit', $data);
}
else
{
$this->load->view('contact/list');
}
} // end function index
} // end class
Template:
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title><?= $title ?></title>
<link rel="stylesheet" type="text/css" href="/css/global.css" />
</head>
<body>
<h2><?= $title ?></h2>
<?= $this->validation->error_string ?>
<?= form_open('contact_update') ?>
<table>
<tr>
<th>First Name</th>
<td><input type="text" size="40" name="first_name" id="first_name" value="<?= $contact->first_name ?>" /></td>
</tr>
<tr>
<th>Last Name</th>
<td><input type="text" size="40" name="last_name" id="last_name" value="<?= $contact->last_name ?>" /></td>
</tr>
<tr>
<th>Address</th>
<td><input type="text" size="40" name="address" id="address" value="<?= $contact->address ?>" /></td>
</tr>
<tr>
<th>City</th>
<td><input type="text" size="40" name="city" id="city" value="<?= $contact->city ?>" /></td>
</tr>
<tr>
<th>State:</th>
<td>
<?= select_field('state_id', $states->result(), 'name', $contact->state_id) ?>
</td>
</tr>
<tr>
<th>Zip</th>
<td><input type="text" size="10" name="zip" id="zip" value="<?= $contact->zip ?>" /></td>
</tr>
</table>
<input type="hidden" name="id" value="<?= $contact->id ?>" />
<input type="submit" value="Submit" />
<a href="/contact">Cancel</a>
</form>
</body>
</html>
This obviously isn't the finished product; nothing gets updated on success. It's a working example, though, so hopefully this will be able to help those who have had similar problems.