0
votes

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

2
Try github.com/CakeDC/search - it does all the above, without all the overhead and in a clean DRY way. It also comes with the PRG pattern that should be used here and also keeps the selected filters.mark

2 Answers

1
votes

I believe you should be able to use $this->params->query in the view to set the default values based on the query

echo $this->Form->create('BillingCenter', array('type' => 'get', 'action' => 'search'));

echo $this->Form->input('BillingCenter.name', array(
    'value' => $this->params->query['name'] //set name param as default value
));

echo $this->Form->input('date', array(
'type'  => 'date',
'selected' => $this->params->query['date'], //set date array as selected value
'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');
0
votes
   public function search() {
        if ($this->request->is('post') || $this->request->is('put')) {
//            /pr($this->request->data); exit;
            $condition = array(
                'User.role' => array('U', 'P'),
                'User.user_status' => array('active', 'lead', 'inactive'),
                'OR' => array(
                    'PersonalInformation.first_name' => $this->request->data['User']['first_name'],
                    'PersonalInformation.last_name' => $this->request->data['User']['last_name'],
                    'PersonalInformation.primary_phone' => $this->request->data['User']['primary_phone'],
                    'PersonalInformation.dob' => $this->request->data['User']['dob'],
                    'User.email' => $this->request->data['User']['email'],
                )
            );
        }