Part of the EllisLab Network
   
 
form validation Field Groups
Posted: 20 June 2009 04:13 PM   [ Ignore ]  
Lab Assistant
Avatar
RankRank
Total Posts:  202
Joined  12-31-2007

Please read this post and let me know if you think this is something that people would need.

Say I have 3 fields for a user to enter his phone number.
phone_area (555)
phone_pre   123
phone_suff 4567

I want to validate all 3 but only show a single error to the right of the phone_suff field.

For simplicity sake, I’m not worrying about min_length or max_length.

$this->form_validation->set_rules('phone_area', 'Phone Area Code', 'is_numeric');
$this->form_validation->set_rules('phone_pre', 'Phone Prefix', 'is_numeric');
$this->form_validation->set_rules('phone_suff', 'Phone Suffix', 'is_numeric');

If any of you have ever used jQuery’s validation plugin you may know that you can define field groups. This is what I’d like to achieve with CI.

I think something along the lines of

$this->form_validation->set_rules('phone_area', 'Phone Area Code', 'is_numeric');
$this->form_validation->set_rules('phone_pre', 'Phone Prefix', 'is_numeric');
$this->form_validation->set_rules('phone_suff', 'Phone Suffix', 'is_numeric');

$this->form_validation->set_field_group(array('phone_area','phone_pre','phone_suff'), 'phone_suff');

That basically states that any errors that come up for any field within the array are applied to the ‘phone_suff’ field.

So after the form_suff field in my HTML I can have <?php echo form_error(‘phone_suff’); ?> and any errors on all 3 fields show up there.

I realize I can achieve this with a callback but I think this is something that may be useful to some people in the future, and I think it should be fairly easy to implement.

Profile
 
 
Posted: 03 July 2009 11:11 PM   [ Ignore ]   [ # 1 ]  
Summer Student
Total Posts:  5
Joined  11-11-2008

I agree wholeheartedly.  I had to write an extension to the form_validator class in order to accomplish some similar functionality.  It would have been nice to have that built in.

Profile
 
 
   
 
 
‹‹ additional mod rewrite help      Assoc data ››
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 819, on March 11, 2010 11:15 AM
Total Registered Members: 120353 Total Logged-in Users: 37
Total Topics: 126480 Total Anonymous Users: 2
Total Replies: 665156 Total Guests: 374
Total Posts: 791636    
Members ( View Memberlist )
Newest Members:  ejsexton82jackmarioRanjanjoyMihai NorthThatchVagariPatient ShareNixnizviVadotlogik