If I follow you correctly:
- The user must specify the start and end dates of the search for queries generated from the form
- You need to confirm these dates, for example:
- end date after start date
- end date not far from start date.
- You want validation errors to appear inside the form (although this is not a save)
Since you want to check these dates, it will be harder to capture them when they are hidden inside your conditions array. I suggest trying to transfer them separately, and then talk to them later:
$this->Model->find('all', array( 'conditions' => array(/* normal conditions here */), 'dateRange' => array( 'start' => /* start_date value */, 'end' => /* end_date value */, ), ));
You can probably 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 search operation, so this may not even work. The idea is that if a form is created using FormHelper , these messages should return to the form fields.
Otherwise, you may need to perform this check in the controller and use Session::setFlash() . if so, you can also get rid of beforeFind and put BETWEEN in the array of conditions with other conditions.
deizel
source share