Validation |
[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> And here's what the CodeIgniter example uses: Code: <h5>Username</h5> 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
[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); View: Code: <p><label for="username">Username:</label><br />
[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 In your template you work as usual: Code: <html>... 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'; Don't be stuck at repopulating with $this->validation. You can use anything to repopulate. $data = $this->validation does the trick in this case.
[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.
[eluser]jasonswett[/eluser]
Here's what I ended up doing. Validation controller: Code: <?php Template: Code: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 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.
|
Welcome Guest, Not a member yet? Register Sign In |