urfusion
urfusion

Reputation: 5501

cakephp validation on multiple dates

I have a form which have add more functionality. user can enter their multiple education. now I want to add a validation rule for the below condition

if user started their first education in 2006 and completed in 2008 then he can not enter second education starting date 2008 or before that

here are my validation rules

 /**
     * 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
            )
        ),
    );

This is what I try and got success only for validating future date condition

 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;
    }

let me know if you want any other information.

Upvotes: 3

Views: 1526

Answers (1)

savedario
savedario

Reputation: 947

Assuming I understood your question correctly, you should have a User model with a hasMany association with DoctorEducation. One approach could be to define in the User model:

var $usedDates = array;
public function beforeValidate($options = array()) {
    $this->usedDates = array();
}

then the validation rules for the DoctorEducation model could use it:

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;
}

The above assumes you will be saving the User model together with the DoctorEducation model.

Upvotes: 1

Related Questions