Search code examples
phpcakephp

CakeDC Search Plugin With Dropdown


I'd like to add a select box for searching with the CakeDC Search Plugin. IE:

<select name="field">
    <option value="email">Search By Email</option>
    <option value="first_name">Search By First Name</option>
</select>

Currently what I have in my VIEW is:

echo $this->Form->create('User', array(
    'url' => array_merge(array('action' => 'index'), $this->params['pass'])
));
echo $this->Form->input('email', array('div' => false, 'empty' => true)); 
echo $this->Form->input('first_name', array('div' => false, 'empty' => true)); 

This works just fine this way, but I'd like to avoid the multiple input boxes and simplify it with a select box. I could hard it (take the value from the select box and combine it with the value from the input box), but there has to be another way to do it...

Here is my User Module:

public $filterArgs = array(
    'email' => array('type' => 'like'),
    'first_name' => array('type' => 'like')
);

And this is my Controller:

public function index() {   
    $this->Prg->commonProcess();
    $this->paginate['conditions'] = $this->User->parseCriteria($this->passedArgs);
    $this->set('users', $this->paginate());
}

Solution

  • i think you are looking for

    echo $this->Form->input('search', array('div' => false, 'empty' => true)); 
    

    and

    public $filterArgs = array(
        'search' => array('type' => 'like', 'field'=>array('email', 'first_name')),
    );
    

    and

    public $presetVars = true;
    

    but you would lose the "AND" of the two inputs in favor of an OR (this is less powerful). if thats ok for you, this would be the way.