![]() |
Yet another form + validation inquiry (sorry :-( ) - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Yet another form + validation inquiry (sorry :-( ) (/showthread.php?tid=3208) |
Yet another form + validation inquiry (sorry :-( ) - El Forum - 09-16-2007 [eluser]stevefink[/eluser] Hey all, So I've decided to create a separate controller/method for editing a form with data populated that a user originally submitted on a prior date. My question is, how do I keep validation for input fields if my markup looks like this: Code: <input type="text" name="ext_color" value="<?= $this->validation->ext_color; ?>" /> In a form without CI type validation, I'd normally put something like: value="<?= $from_db_value ?>" etc. There anyway I can end up doing this without copying over a new view? Thanks all. :-) - sf Yet another form + validation inquiry (sorry :-( ) - El Forum - 09-16-2007 [eluser]Colin Williams[/eluser] This is why the validation class needs a set_values() method. I think someone whipped this up in a library, actually. Try a search. Yet another form + validation inquiry (sorry :-( ) - El Forum - 09-16-2007 [eluser]Derek Allard[/eluser] Here's how I generally go about it. Code: <input name="company_name" type="text" id="company_name" value="<?php echo ($this->validation->company_name) ? or more stretched out: if there is validation data, use that, else, lead the row from the database. Yet another form + validation inquiry (sorry :-( ) - El Forum - 09-16-2007 [eluser]Colin Williams[/eluser] That makes for nasty view code, in my opinion. I think that logic makes more sense happening in the Validation class. For example: Code: $fields['username'] = 'Username'; And by your example, the logic could break down if someone had a field that was populated with a value, say 'foo.' They attempt to set this value to blank, generate a validation error elsewhere, when the form is reloaded, they would see 'foo' in the field they left blank. I believe you would need ($this->validation->company_name !== FALSE) ? [...] Yet another form + validation inquiry (sorry :-( ) - El Forum - 09-17-2007 [eluser]stevefink[/eluser] Thanks for the replies folks. I'll search for the forums to try to find the wrapper utility that's been written for the Validation class. If I'm not successful in finding it, I'll spend some time putting together something and share it with the community in the Wiki. - sf Yet another form + validation inquiry (sorry :-( ) - El Forum - 10-09-2007 [eluser]ariok[/eluser] did'you find some soulutions? Yet another form + validation inquiry (sorry :-( ) - El Forum - 10-09-2007 [eluser]stevefink[/eluser] ariok - I haven't had time to do the search. I'm currently using the methods suggest by Derek above. hth - sf Yet another form + validation inquiry (sorry :-( ) - El Forum - 10-09-2007 [eluser]phester[/eluser] Steve, You might want to check out this post. Let me know if it helps! Thanks, Yet another form + validation inquiry (sorry :-( ) - El Forum - 10-11-2007 [eluser]parrots[/eluser] I needed a function similar to the set_value one described above, so I went ahead and added it to the Validation class. I didn't see this posted anywhere else, sorry if it's a dupe of anyone's previous code. It's ideal for an edit form where you might want to pre-populate a form with data from a database but once they submit (and possibly zero out something) you don't want to use that data anymore. Figured it'd be worth posting here for people, even if it was a pretty easy mod... Add this global variable up with the rest of them in system/libraries/validation.php: Code: var $_defaults = array(); Replace the foreach block in the set_fields method with this (as per Darren Inwood's somewhat similar mod here): Code: foreach($this->_fields as $key => $val) Then add the following method anywhere in the class: Code: // -------------------------------------------------------------------- Then use it as such in your controller: Code: $rules['email'] = "trim|required|valid_email|xss_clean"; This makes it so you can always set your form values to $this->validation->inputname, like so, and you'll never have to worry about where the data is coming from in your view. Code: <label for="name">Name</label> Yet another form + validation inquiry (sorry :-( ) - El Forum - 10-12-2007 [eluser]phester[/eluser] Parrots, that's a great fix, thanks for sharing! I usually try to avoid changing the CI's existing libraries to make my application forward compatible with future versions of CI. Another quick fix I thought about is this: Say you have an text field named "email", on your first load of the form, do this: Code: $this->validation->email= $this->session->userdata('email'); This works as well - however you solution is more elegant since it prep's the data to be loaded for the form as well. I'm curious to see what everyone's thoughts are for this non-obtrusive approach. |