My index.ctp has a search form with 2 input fields. One is a name field based on model field BillingCenter.name, and another is a date field which is not linked to a model field. Pressing search will pass parameters via URL to the action "Search".
index.ctp
echo $this->Form->create('BillingCenter', array('type' => 'get', 'action' => 'search'));
echo $this->Form->input('BillingCenter.name');
echo $this->Form->input('date', array(
'type' => 'date',
'label' => 'Date',
'empty' => TRUE,
'minYear' => 2000,
'dateFormat' => 'DMY',
'maxYear' => date('Y'),
'minYear' => date('Y') - 10
# default order m/d/y
));
echo $this->Form->end('Search');
When I click search it generates not so nice-looking URL like this:
http://localhost/bm/billing_centers/search?name=jo&date%5Bday%5D=01&date%5Bmonth%5D=03&date%5Byear%5D=2014
This is the simple "Search" action in my controller.
public function search() {
$this->view = 'index';
if (!isset($this->request->query['name']) OR !isset($this->request->query['date'])) {
throw new BadRequestException();
}
//concatenate date from URL
$effectivedate = $this->request->query['date']['year'] . $this->request->query['date']['month'] . $this->request->query['date']['day'];
//assemble the search conditions
$searchfilter = array(
'BillingCenter.name LIKE' => '%' . $this->request->query['name'] . '%',
'BillingCenter.startdate <=' => $effectivedate,
'BillingCenter.enddate >=' => $effectivedate
);
$this->set('billingcenters', $this->BillingCenter->find('all', array('conditions' => $searchfilter )));
The search seems to work fine (please comment if anything i do above is bad practice). The search leads back to the same index.ctp and shows the correct search results.
HOWEVER, I need the search form input field values to default to the values submitted in the previous search, how can i get these values (presumably from the URL), and auto populate the text input and date input fields?
Cheers Kevin