Dropdown with default value and option "all"

2

Using CakePHP in my project, I created a dropdown to use to search the database for values and strings .

I need to create an "all" option to search all the results and to be the "default" option of this dropdown . How can I do this?

Code:

<?php echo $this->Form->create('RegistroHorario', array('class' => 'form-horizontal bucket-form', 'autocomplete' => 'off')); ?>  
<div class="col-lg-3">  
<?php echo $this->Form->select('RegistroHorario.cliente_id', $clientes, array('empty' => false ,'div' => false,'label'=>false, 'class' => 'form-control m-bot15', 'width' => '10'));?>  
</div><div class="col-lg-3">  
<?php echo $this->Form->select('RegistroHorario.user_id', $usuarios, array('empty' => false ,'div' => false,'label'=>false, 'class' => 'form-control m-bot15', 'width' => '10'));?>   
</div>  
<input type="submit" class="btn btn-success" value="Buscar" />  
<?php echo $this->Form->end(); ?>
    
asked by anonymous 09.01.2015 / 16:26

1 answer

2

In the controller, you check whether the method is POST . When it is not, you set the value directly in the data property of the Cakephp controller.

See:

<?php
    if ($this->Request->is(array('post', 'put'))) {
        // Operação para salvar os dados no banco
    } else {
        $this->data['RegistroHorario']['campo_select'] = "valor igual ao quer que seja o Default";
    }

This view in the English SO .

Update

Since there is also a need to append "all" to select, I believe that if a variable of type Array is passed to fill the option of select , then you would just do this:

$this->Form->select('RegistroHorario.usuario_id', array('valor' => 'Todos') + $usuarios)
    
09.01.2015 / 16:33