Welcome Guest, Not a member yet? Register   Sign In
Validation
#5

[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">
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;&lt;?= $title ?&gt;&lt;/title&gt;
&lt;link rel="stylesheet" type="text/css" href="/css/global.css" /&gt;
&lt;/head&gt;
&lt;body&gt;

<h2>&lt;?= $title ?&gt;</h2>

&lt;?= $this->validation->error_string ?&gt;

&lt;?= form_open('contact_update') ?&gt;

<table>
  <tr>
    <th>First Name</th>
    <td>&lt;input type="text" size="40" name="first_name" id="first_name" value="&lt;?= $contact-&gt;first_name ?&gt;" /></td>
  </tr>
  <tr>
    <th>Last Name</th>
    <td>&lt;input type="text" size="40" name="last_name" id="last_name" value="&lt;?= $contact-&gt;last_name ?&gt;" /></td>
  </tr>
  <tr>
    <th>Address</th>
    <td>&lt;input type="text" size="40" name="address" id="address" value="&lt;?= $contact-&gt;address ?&gt;" /></td>
  </tr>
  <tr>
    <th>City</th>
    <td>&lt;input type="text" size="40" name="city" id="city" value="&lt;?= $contact-&gt;city ?&gt;" /></td>
  </tr>
  <tr>
    <th>State:</th>
    <td>
      &lt;?= select_field('state_id', $states->result(), 'name', $contact->state_id) ?&gt;
    </td>
  </tr>
  <tr>
    <th>Zip</th>
    <td>&lt;input type="text" size="10" name="zip" id="zip" value="&lt;?= $contact-&gt;zip ?&gt;" /></td>
  </tr>
</table>

&lt;input type="hidden" name="id" value="&lt;?= $contact-&gt;id ?&gt;" />

&lt;input type="submit" value="Submit" /&gt;
<a href="/contact">Cancel</a>

&lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt;

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.


Messages In This Thread
Validation - by El Forum - 10-22-2007, 12:04 PM
Validation - by El Forum - 10-22-2007, 01:24 PM
Validation - by El Forum - 10-22-2007, 02:07 PM
Validation - by El Forum - 10-22-2007, 02:21 PM
Validation - by El Forum - 10-22-2007, 03:01 PM
Validation - by El Forum - 10-22-2007, 03:30 PM



Theme © iAndrew 2016 - Forum software by © MyBB