I’ve been seraching for weeks how to do that but didn’t find any good answer. I mean I thought it was supposed to be easy to do that, but not at all. Here’s the thing, I have to ask the user to choose a month interval so I can show the right data to him. Since it’s not related to any table I have on my database, I just did this:
<?php
echo $this->Form->create(false);
echo $this->Form->month('m1', null, array('monthNames' => false));
echo ' until ';
echo $this->Form->month('m2', null, array('monthNames' => false));
echo $this->Form->end('Choose');
?>
And then created a simple rule at the model (there’s only a rule for one field, for test purpose):
var $validate = array('m1' => array('rule' => 'notEmpty', 'required' => true, 'message' => 'Error!'));
Here’s my controller:
function choose_date ($id = null) {
if (!empty($this->data)) {
$this->Frequency->set($this->data);
if ($this->Frequency->validates()) {
$fD = $this->data['m1']['month'].'/'.date('Y');
$tD = $this->data['m2']['month'].'/'.date('Y');
$this->Session->write('Frequency.fromDate', $fD);
$this->Session->write('Frequency.toDate', $tD);
$this->redirect(array('action' => 'view'));
}
} else { $this->Session->write('Worker.id', $id); }
}
So what happens is that it just freezes at the “choose_date” view, doesn’t matter if do or don’t select anything on the list. How can I validate it? It’s probably a simple task but it’s getting me crazy!
Solved it. I just needed to use the rule ‘multiples’ and give the values I wanted to accept. 🙂