0

Ok, So I ave implemented a Date of Birth into the user registration. What I want to do now is take that date of birth and check to see if they are above a certain age (13) before they register. They way I did DOB is kinda weird, but it works. I have 3 field dob1, dob2, dob3. CodeIgniter: Tank Auth, Adding Date of Birth Issues here is how i implemented it if anyone is interested. Anyway, this is what I have been trying so far: EDIT: the syntax the user puts in is mm dd yyyy

function is_old_enough($input, $dob1, $dob2) {
          $dob = $dob1.$dob2.$input;
          $date = date('md').(date('Y')-13);
          if ((int)$dob < (int)$date)
            $this->form_validation->set_message('is_old_enough', 'You are not old enough to have an account on this site.');
          return $input;
        }

And here is what is inside the register() function.

$this->form_validation->set_rules('dob1', 'Date of Birth Month', 'trim|required|xss_clean|exact_length[2]');
$this->form_validation->set_rules('dob2', 'Date of Birth Day', 'trim|required|xss_clean|exact_length[2]');
$this->form_validation->set_rules('dob3', 'Date of Birth Year', 'trim|required|xss_clean|exact_length[4]|callback_is_old_enough[dob1||dob2]');

Am I close? Am i way off? Anyone help? Right now all it does is pretends like I never created this callback and puts the user in even if the user is too young. I know it is calling the function correctly as I had some issues with the variables. Help?

EDIT: Brendan's answer helped me a lot, but the main issue was a logic error. So here is how I have it working right now:

//Check if user is old enough
function is_old_enough($input) {
    $dob = $this->input->post('dob3').$this->input->post('dob1').$this->input->post('dob2');
    $date = (date('Y')-13).date('md');
    if ((int)$dob > (int)$date) {
        $this->form_validation->set_message('is_old_enough', 'You are not old enough to register on this site.');
        return FALSE;
    }
    return TRUE;
}


$this->form_validation->set_rules('dob1', 'Date of Birth Month', 'trim|required|xss_clean|exact_length[2]');
$this->form_validation->set_rules('dob2', 'Date of Birth Day', 'trim|required|xss_clean|exact_length[2]');
$this->form_validation->set_rules('dob3', 'Date of Birth Year', 'trim|required|xss_clean|exact_length[4]|callback_is_old_enough[]');
Community
  • 1
  • 1
ageoff
  • 2,798
  • 2
  • 24
  • 39

1 Answers1

1

First of all, you can only pass up to two arguments in a callback. Second, if you return a non-boolean value from a callback, whatever is returned will replace the value of the field you ran the callback on.

If you're trying to check if something is valid, the way it works (essentially) is:

function _callback_for_field($input)
{
    // check if $input is valid based on your own logic
    if($input == YOUR_LOGIC_HERE)
    {
        return TRUE;
    }
    return FALSE;
}

But for what you're doing, specifically:

// Birthdate rules
$this->form_validation->set_rules('birthdate-month','Birthdate Month','required|is_natural_no_zero|greater_than[0]|less_than[13]');
$this->form_validation->set_rules('birthdate-day','Birthdate Day','required|is_natural_no_zero|greater_than[0]|less_than[32]');
$this->form_validation->set_rules('birthdate-year','Birthdate Year','required|is_natural_no_zero|greater_than[1930]|less_than['.(date("Y") - 18).']');

I purposely don't go to great lengths to prevent someone that's barely under the age of 18 to register, because if they want to, they'll do it anyway. It is impossible to restrict registration based on age if the person is determined to, and since you're not checking some government database on citizens, it's not really within your scope of responsibility. A simple age check is all that's required.

I had a second thought -- If you still want to accurately check, you can reference $this->input->post() for each of the fields in your callback function. You can even run the callback function with no arguments, since you will be circumventing that restriction.

Brendan
  • 4,565
  • 1
  • 24
  • 39
  • OK,I got it figured out. I completely forgot about returning true or false so thank you for that. The other thing was the input->post() was very helpful so i did not have to worry about passing anything, another useful tip. The overall problem however was a logic error. I was checking if is was < when it should be >. Silly me (and I always tested with 2010 as my year so IDK if i ever had it correct. I understand I can prevent anyone from making an account, but i like the practice and learnign experiences. Thanks again for your help! – ageoff Sep 14 '12 at 21:08
  • also, another problem was the order. They way I had my date formated was mdy, which meant it was checking the month vs year. So i switched the order arround to ymd to properly tell which date comes first between date and dob – ageoff Sep 14 '12 at 21:17