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

#1
[eluser]jasonswett[/eluser]
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:
Code:
<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>

And here's what the CodeIgniter example uses:
Code:
<h5>Username</h5>
&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.

Thanks,
Jason

#2
[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.

Controller:
Code:
$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;
}

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

#3
[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:

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

$data = $record_found;
$this->load->view('my_template',array('data'=>$data));

In your template you work as usual:

Code:
&lt;html&gt;...

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

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

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

if($valid){
    // do something
}else{
    // do something else
    // resend data to the view
    $this->load->view('my_template',array('data'=>$data));
}

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

#4
[eluser]jasonswett[/eluser]
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.

#5
[eluser]jasonswett[/eluser]
Here's what I ended up doing.

Validation controller:
Code:
&lt;?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.

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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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