![]() |
Date validation CI 1.7 - 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: Date validation CI 1.7 (/showthread.php?tid=14627) |
Date validation CI 1.7 - El Forum - 01-09-2009 [eluser]codex[/eluser] This has me puzzled for days so I hope someone can shed some light on the issue. Suppose you'd like to validate a user's date of birth, which consists of 3 dropdowns 'day, month, year'. All 3 combined make up 'date_of_birth'. If one of the 3 selects is not filled in, the rules that apply to date_of_birth spring into action. (does this make any sense?) You can't do Code: $this->form_validation->set_rules('date_of_birth', 'Birthday', 'required'); How do you check something like this? Date validation CI 1.7 - El Forum - 01-09-2009 [eluser]xwero[/eluser] Why not validate them one by one instead of wanting to join them for the validation? And use static messages like : The month of your birthday isn't defined. Date validation CI 1.7 - El Forum - 01-09-2009 [eluser]codex[/eluser] [quote author="xwero" date="1231528593"]Why not validate them one by one instead of wanting to join them for the validation?[/quote] That is an option yes, but what if you'd want to do it like I asked? Is that even possible? Date validation CI 1.7 - El Forum - 01-09-2009 [eluser]Mat-Moo[/eluser] Probably not the best way, bit I used a callback on the day validation, and directly took the year/month Code: function check_date($day) Date validation CI 1.7 - El Forum - 01-09-2009 [eluser]xwero[/eluser] Yes but it's messy because you have to assign rules to a input field and you have to do three validations at once and specify which error occurs. A solution could be to create a hidden field with the name birthday you give the value fake_input for example and you create a birthdate_selected callback Code: function birthday_selected() Date validation CI 1.7 - El Forum - 01-09-2009 [eluser]Mat-Moo[/eluser] Well considering I'm using a drop down for month and year, it's pretty hard for them to be wrong. However things like the 30th Feb are of course invalid hence why only on the day. Date validation CI 1.7 - El Forum - 01-09-2009 [eluser]xwero[/eluser] Mat-Moo's solution is better but the day select can not have an empty value otherwise the date doesn't get validated at all. Date validation CI 1.7 - El Forum - 01-09-2009 [eluser]Mat-Moo[/eluser] Of course you could add a bit of javascript and check the date before the form is submitted. (PS I also use a drop down for day as well!) Date validation CI 1.7 - El Forum - 01-09-2009 [eluser]codex[/eluser] Thank you guys! This gives me a few ideas on how to tackle. Date validation CI 1.7 - El Forum - 01-09-2009 [eluser]helmutbjorg[/eluser] Not sure if i fully understand your problem... but this may help You CAN do without the actual element in the form Code: $this->form_validation->set_rules('date_of_birth', 'Birthday', 'required'); IF you do this before it (not tested by i'm pretty sure) Code: $_POST['date_of_birth'] = $this->input->post('day').' '.$this->input->post('month').' '.$this->input->post('year'); So in other words concatenate each drop down together (however you need to) and trick the form validation into thinking you posted that field. |