I have the following validation rule in my model:
'dob' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'Date of Birth is required'
),
'age' => array(
'rule' => array('comparison', '>=', 13),
'message' => 'You must be over 13 years old'
)
)
What I’m trying to achieve is validate that the user is over 13 years old…
The date is created like so:
<?php echo $this->Form->input('Profile.dob', array('label' => 'Date of Birth'
, 'dateFormat' => 'DMY'
, 'minYear' => date('Y') - 110
, 'maxYear' => date('Y') - 13)); ?>
How do I do it though? As the saved data is a date and not a whole number so my comparison won’t work… Looking for simplest solution here without replying on plugins or other external assets and just some simple code if possible.
Thanks.
EDIT: So based on the comments below I have added:
public function checkDOB($check) {
return strtotime($check['dob']) < strtotime();
}
But what do I put in the strtotime to check the age is above or EQUAL to 13?
Create a custom validation rule in your model:
This uses a neat feature of strtotime that lets you easily do date calculations on a particular date.
To use the rule: