• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5

I've gone through most of the CodeIgniter Form Validation User Guide Page and it worked just fine. But what if you're dealing not with a sign-up form but a form meant to update values in a database?

Here's what the XHTML for one of my fields would look like:
  <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>

And here's what the CodeIgniter example uses:
&lt;input type="text" name="username" value="&lt;?=$this-&gt;validation->username;?&gt;" size="50" />

Using "$this->validation->whatever" is fine for when the user submits an incomplete form, then gets redirected back to the same form, but it wouldn't be useful on the initial page load.

Is there a reasonable solution to this problem? It seems like a pretty common need and I'm surprised I haven't been able to find the answer yet.


[eluser]Michael Wales[/eluser]
You would select that data from your database and pass it to the view - just like you would any other data.

$query = $this->db->select('username')->getwhere('users', array('id'=>'1'), 1, 0);
if ($query->num_rows() == 1) {
  $row = $query->row();
  $this->data->user->username = $row->username;

<p><label for="username">Username:</label><br />
&lt;input type="text" name="username" value="&lt;?= $user-&gt;username; ?&gt;" size="50" /></p>

[eluser]Michael Ekoka[/eluser]
In the case of an initial display, you fetch your data as usual using the active record, orm or whatnot. Then you reassign that data to a variable. You then pass that variable to the template:

// we'll assume you've ran all the where, sort, limit, etc
$record_found = $this->db->get('user_table');

$data = $record_found;

In your template you work as usual:


  <th>First Name</th>
  <td>&lt;input type="text" size="40" name="first_name" id="first_name" value="&lt;?=$data-&gt;first_name ?&gt;" /></td>

Now after submitting, you proceed as you would normally with your validation. Except that at the end you assign the $validation to $data:

$rules['user_name'] = 'somerules';
$fields['user_name'] = 'User name';
$valid = $this->validation->run();
$data = $this->validation;

    // do something
    // do something else
    // resend data to the view

Don't be stuck at repopulating with $this->validation. You can use anything to repopulate. $data = $this->validation does the trick in this case.

Thanks for the replies. CodeIgniter's solution looks like the one I was looking for. My main problem was that, on initial load, I had to use $data and, on return from validation, I had to use $this->validation, but if I can assign $this->validation to $data, great.

Here's what I ended up doing.

Validation controller:

class Contact_update extends Controller {

  function index()
    $this->load->helper(array('form', 'url'));


    // 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.

    // 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.

    // 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);

  } // end function index

} // end class

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
&lt;title&gt;&lt;?= $title ?&gt;&lt;/title&gt;
&lt;link rel="stylesheet" type="text/css" href="/css/global.css" /&gt;

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

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

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

    <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>
    <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>
    <td>&lt;input type="text" size="40" name="address" id="address" value="&lt;?= $contact-&gt;address ?&gt;" /></td>
    <td>&lt;input type="text" size="40" name="city" id="city" value="&lt;?= $contact-&gt;city ?&gt;" /></td>
      &lt;?= select_field('state_id', $states->result(), 'name', $contact->state_id) ?&gt;
    <td>&lt;input type="text" size="10" name="zip" id="zip" value="&lt;?= $contact-&gt;zip ?&gt;" /></td>

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

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



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.

[eluser]Michael Ekoka[/eluser]
looks about right.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

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