Welcome Guest, Not a member yet? Register   Sign In
Date validation CI 1.7
#1

[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');
since date_of_birth isn't an input and is therefore not posted.

How do you check something like this?
#2

[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.
#3

[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?
#4

[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)
    // checkes that the date entered is valid - edit and add - could do with making this more generic
    {
        $check=checkdate($this->input->post('fmonth'),$day,$this->input->post('fyear'));
        if (!$check)
        {
            $this->form_validation->set_message('check_date','This is not a legal date');
            return FALSE;
        }
        else
        {
            return TRUE;
        }
#5

[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()
{
   $not_selected = array();

   if( ! isset($_POST['year']))
   {
      $not_selected[] = 'year';
   }

   if( ! isset($_POST['month']))
   {
      $not_selected[] = 'month';
   }

   if( ! isset($_POST['day']))
   {
      $not_selected[] = 'day';
   }

   if(count($not_selected) > 0)
   {
       $this->form_validation->set_message('The '.implode(', ',$not_selected).' of the birthday are not defined');
       return FALSE;
   }

   return TRUE;
}
#6

[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.
#7

[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.
#8

[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!)
#9

[eluser]codex[/eluser]
Thank you guys! This gives me a few ideas on how to tackle.
#10

[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');
$this->form_validation->set_rules('date_of_birth', 'Birthday', 'required');

So in other words concatenate each drop down together (however you need to) and trick the form validation into thinking you posted that field.




Theme © iAndrew 2016 - Forum software by © MyBB