![]() |
Validating groups of inputs - 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: Validating groups of inputs (/showthread.php?tid=32561) |
Validating groups of inputs - El Forum - 07-27-2010 [eluser]vertmonkee[/eluser] I have a form where a user is entering a location. The name and description are required so I have set my rules like this Code: $rules['locationName'] = "required"; They can then choose a coordinate system from three choices. 1. Decimal latitude / longitude 2. Grid reference 3. Latitude / longitude How would I go about setting a rule for this? Thanks for any help Validating groups of inputs - El Forum - 07-27-2010 [eluser]Ihab Khattab[/eluser] what I understand you have problem in setting rules for select for example if your markup for select something like Code: <select name="coor-sys"> you should write something like Code: $config = array( I modified the code you provide as I think it won't work. for more example about passing rules as an array you may check http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#validationrulesasarray Validating groups of inputs - El Forum - 07-28-2010 [eluser]vertmonkee[/eluser] Thanks for the suggestion. Unfortunately that's not how my form is set up. What I have is the following Code: <label for="locationDecimalLatitude">Decimal latitude</label> The user must complete at least one combination of locationDecimalLatitude and locationDecimalLongitude locationLatitude, locationLongitude and locationEastOrWest locationGridReferenceLetter and locationGridReferenceNumber Hope this explains the problem clearer. I'd like to be able to somehow include this in my validation rules as I check the form is valid with Code: if ($this->validation->run() != FALSE) { Thanks Validating groups of inputs - El Forum - 07-28-2010 [eluser]Ihab Khattab[/eluser] hmmm this gets now more interesting ![]() you can use arrays as fileds name http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#arraysasfields For example your markup would be something like Code: <fieldset> Note: fieldset and legend aren't required if you don't want to put them I just put them to make groups Your validation rules would be something like Code: $config = array( And you would for sure pass them as Code: $this->form_validation->set_rules($config); so you would have comb1[],comb2[],comb3[] as your groups of input as Codeigniter will treat each as one array with integer index start from 0 and you could set rule for each individual field of this group for example if you want the field with label locationDecimalLongitude -which is the second input in comb1- to be max length of 3 you should modify your rules to be like Code: $config = array( The problem here you won't have your inputs named as you want them to be meaningful you have to refer to them by index comb1[1] and so on Hope this help Validating groups of inputs - El Forum - 07-29-2010 [eluser]vertmonkee[/eluser] Thanks very much Ihab, That is a very good solution. I'll have a go at implementing it and report back. Thanks for your help Validating groups of inputs - El Forum - 07-29-2010 [eluser]Ihab Khattab[/eluser] You are welcome vertmonkee But I think there will be a problem ![]() When user submit only one combination he'll still get error that other combinations are required there won't be the optional thing Sorry this was my fault ![]() Validating groups of inputs - El Forum - 07-29-2010 [eluser]vertmonkee[/eluser] I was thinking about creating a callback for the validation. The problem I have with that is displaying the error in my view. E.g. if I have a callback function as so Code: function validateGroups() { And then trigger this from one of the inputs like this Code: $rules['locationDecimalLatitude] = "numeric|callback_validateGroups"; In my view how would I be able to tell if the error applies to the decimal latitude not being numeric or there not being a valid group completed. Validating groups of inputs - El Forum - 07-29-2010 [eluser]Ihab Khattab[/eluser] You can set error message for your callback using Code: $this->form_validation->set_message() inside your callback function like the example in user guide http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#callbacks but did your validateGroups() check if there is at least one group completed,so you could solve this problem ? If so, could you please share your code with us ? Validating groups of inputs - El Forum - 07-29-2010 [eluser]vertmonkee[/eluser] In the callback function I would have something like the following Code: // Check the decimal lat and long Which should work. I'm just unsure on displaying the error message in my view. If the error message applies to there not being a valid group it needs to be in one place on the view or if it just applies to the locationDecimalLatitude it needs to be displayed next to this input. The only way I can think is to check the value of the string in the error message but this seems like a horrible way of doing it. E.g. Code: <!-- Above all groups --> Validating groups of inputs - El Forum - 07-29-2010 [eluser]Ihab Khattab[/eluser] You can show errors Individually using form_error() http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#individualerrors for example above your groups you would write Code: <?php echo form_error(); ?> 3 times, every time you pass your group name so if there is an error in group it would echo it if there is no error it won't show any thing and next to each input you would write Code: <?php echo form_error(); ?> but I think you still should use arrays as input names so you can use form_error('comb1[]') for a group and use form_error('comb1[n]') to echo it next to input.and also using it to set your rules I'll try to write some code to make things clearer |