问题
i have two fields in my form that should correspond to each other.If a users enters the date of birth,in the D.O.B field,he/she must not be allowed to enter a period of residence greater than the D.O.B
my two fields in the add.ctp are as follows
echo $this->Form->input('DOB',array('label' => 'Date of birth*', 'minYear' => 1900, 'maxYear' => 2000));
echo $this->Form->input('period_of_residence', array('label' =>'Period of residence in Zimbabwe'));
So now i don't know how i validate the two so that the user cannot enter a period of residence greater than the age.Even if it validates on submission i like it.
回答1:
You can create your own custom validation function in your model like so:
class MyModel extends AppModel {
public $validate = array(
'DOB' => array(
'rule' => 'checkDOB',
'message' => 'DOB cannot be greater than period of residence.'
)
);
public function checkDOB($check) {
return strtotime($check['DOB']) < strtotime($this->data['MyModel']['period_of_residence']);
}
}
来源:https://stackoverflow.com/questions/11209968/cakephp-how-to-validate-my-d-o-b-field-so-that-the-age-will-not-be-greater-than