我有一个添加了更多功能的表单。用户可以输入他们的多重教育。现在我想为以下条件添加验证规则
如果用户在2006年开始他们的第一次教育并在2008年完成 然后他无法进入2008年或之前的第二次教育 的那
这是我的验证规则
/**
* Validation
*
* @var array
* @access public
*/
public $validate = array(
'degree_type_id' => array(
'notEmpty' => array(
'rule' => 'notEmpty',
'message' => 'This field cannot be left blank.',
'last' => true,
),
'is_unique_degree' => array(
'rule' => 'is_unique_degree',
'message' => 'You have added same course multiple time.',
'last' => true,
),
),
'college_hospital' => array(
'notEmpty' => array(
'rule' => 'notEmpty',
'message' => 'This field cannot be left blank.',
'last' => true,
),
'size' => array(
'rule' => array('maxLength', 255),
'message' => 'This field must be no larger than 255 characters long.'
),
),
'year_passing' => array(
'notEmpty' => array(
'rule' => 'notEmpty',
'message' => 'This field cannot be left blank.',
'last' => true,
),
),
'start_date' => array(
'notEmpty' => array(
'rule' => 'notEmpty',
'message' => 'This field cannot be left blank.',
'last' => true,
),
'dateRule' => array(
'rule' => array('date', 'ymd'),
'message' => 'Enter a valid date in MM/YYYY format.',
'allowEmpty' => true
),
'validateDuration' => array(
'rule' => 'validateDuration',
'message' => 'This field cannot be left blank.',
'last' => true,
),
),
'end_date' => array(
'notEmpty' => array(
'rule' => 'notEmpty',
'message' => 'This field cannot be left blank.',
'last' => true,
),
'dateRule' => array(
'rule' => array('date', 'ymd'),
'message' => 'Enter a valid date in MM/YYYY format.',
'allowEmpty' => true
),
'validateEndDate' => array(
'rule' => 'validateEndDate',
'message' => 'End date should be greater than start date.',
'allowEmpty' => true
)
),
);
这是我尝试并且仅为验证未来日期条件而获得成功
public function validateDuration() {
$startDate = $this->data['DoctorEducation']['start_date'];
$endDate = $this->data['DoctorEducation']['end_date'];
if (strtotime(date('Y-m-d')) < strtotime($startDate)) {
return __('Education duration will not be future date');
}
if (strtotime(date('Y-m-d')) < strtotime($endDate)) {
return __('Education duration will not be future date');
}
//pr($this->data);
return true;
}
如果您需要任何其他信息,请与我们联系。
答案 0 :(得分:1)
假设我正确理解了您的问题,您应该拥有一个与DoctorEducation有一个hasMany关联的User模型。一种方法可以是在用户模型中定义:
var $usedDates = array;
public function beforeValidate($options = array()) {
$this->usedDates = array();
}
然后,DoctorEducation模型的验证规则可以使用它:
public function validateStart( $dataToValidate ) {
foreach($this->ParentModel->usedDates as $usedDate) {
// Perform whatever check you need to ensure periods
// don't overlap and return errors
}
$key = count($this->ParentModel->usedDates);
$this->ParentModel->usedDates[$key]['start_date'] = $dataToValidate['start_date'];
return true;
}
public function validateEnd( $dataToValidate ) {
foreach($this->ParentModel->usedDates as $usedDate) {
// Perform whatever check you need to ensure periods
// don't overlap and return errors
}
$key = count($this->ParentModel->usedDates) - 1;
$this->ParentModel->usedDates[$key]['end_date'] = $dataToValidate['end_date'];
return true;
}
以上假设您将使用DoctorEducation模型将用户模型保存在一起。