If I am following you correctly:
- The user must specify start/end dates for find queries generated from a form
- You need to validate these dates so that, for example:
- end date after start date
- end date not centuries away from start date
- You want validation errors appearing inline within the form (even though this isn't a save)
Since you want to validate these dates they will be harder to grab when they are tucked away inside your conditions array. I suggest trying to pass these in separately and then dealing with them later:
$this->Model->find('all', array(
'conditions' => array(/* normal conditions here */),
'dateRange' => array(
'start' => /* start_date value */,
'end' => /* end_date value */,
),
));
You should hopefully be able to handle everything else in the beforeFind
filter:
public function beforeFind() {
// perform query validation
if ($queryData['dateRange']['end'] < $queryData['dateRange']['start']) {
$this->invalidate(
/* end_date field name */,
"End date must be after start date"
);
return false;
}
/* repeat for other validation */
// add between condition to query
$queryData['conditions'][] = array(
'Model.dateField BETWEEN ? AND ?' => array(
$queryData['dateRange']['start'],
$queryData['dateRange']['end'],
),
);
unset($queryData['dateRange']);
// proceed with find
return true;
}
I have not tried using Model::invalidate()
during a find operation, so this might not even work. The idea is that if the form is created using FormHelper
these messages should make it back next to the form fields.
Failing that, you might need to perform this validation in the controller and use Session::setFlash()
. if so, you can also get rid of the beforeFind
and put the BETWEEN
condition array in with your other conditions.