CakePHP - 使用以前的搜索值填充搜索表单

时间:2014-03-05 13:25:06

标签: forms date cakephp search default-value

我的index.ctp有一个包含2个输入字段的搜索表单。一个是基于模型字段BillingCenter.name的名称字段,另一个是未链接到模型字段的日期字段。 按搜索会通过URL将参数传递给“搜索”操作。

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');

当我点击搜索时,它会生成不那么好看的网址:

http://localhost/bm/billing_centers/search?name=jo&date%5Bday%5D=01&date%5Bmonth%5D=03&date%5Byear%5D=2014

这是我控制器中的简单“搜索”操作。

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     )));

搜索似乎工作正常(如果我上面做的任何事情都是不好的做法请评论)。 搜索返回到相同的index.ctp并显示正确的搜索结果。

但是,我需要将搜索表单输入字段值默认为上一次搜索中提交的值,如何获取这些值(可能来自URL),并自动填充 文字输入日期输入字段?

干杯 凯文

2 个答案:

答案 0 :(得分:1)

我相信您应该能够在视图中使用$this->params->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');

答案 1 :(得分:0)

   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'],
                )
            );
        }