htmlcakephpdrop-down-menuauto-populate

CakePHP normal input box pre populates but not select box?


I'm using Cake's form helper, and it is supposed to pre-populate when I set $this->request->data to something in my controller. It is pre-populating for normal type="text" input boxes, but not for type="select". Anyone know why?

If I pr($this->request->data) in my view I get this result:

Array
(
  [EventInvoiceHardsurface] => Array
    (
        [id] => 7868
        [expediting_notes] => Fake expiditing notes
        [installation_notes] => Fake installation notes.  
    )

  [PurchasingProduct] => Array
    (
        [style_number] => BDP
        [style_name] => DUNDEE PLANK 3 1/4
    )

  [PurchasingProductColor] => Array
    (
        [name] => CB1230 SEASHELL
    )

)

This does NOT pre-populate

                            <?=$this->Form->input('PurchasingProductColor.name', array('type' => 'select', 'label' => 'Product Color', 'div' => false, 'placeholder' => 'Color Name', 'class' => 'input-medium', 'disabled' => 'disabled', 'empty' => true));?>

But this DOES

                            <?=$this->Form->input('PurchasingProductColor.name', array('type' => 'text', 'label' => 'Product Color', 'div' => false, 'placeholder' => 'Color Name', 'class' => 'input-medium', 'disabled' => 'disabled', 'empty' => true));?>

I've tried removing the 'empty' => true and removing the placeholder and removing the disabled, but none of those things made a difference.

Any ideas guys? Thanks.

Edit:

I just ended up using this.

                            <?=$this->Form->input('PurchasingProductColor.name', array('type' => 'select', 'label' => 'Product Color', 'div' => false, 'placeholder' => 'Color Name', 'class' => 'input-medium', 'options' => array((!empty($this->request->data['PurchasingProductColor']['id']) ? $this->request->data['PurchasingProductColor']['id'] : '') => (!empty($this->request->data['PurchasingProductColor']['name']) ? $this->request->data['PurchasingProductColor']['name'] : ''))));?>

I lose the empty => true functionality, and the disabled functionality, but I will controls those via JavaScript.

Thanks.


Solution

  • You have to provide options

    <?=$this->Form->input('PurchasingProductColor.name', 
    array('type' => 'select', 'label' => 'Product Color', 'div' => false, 
    'placeholder' => 'Color Name', 'class' => 'input-medium', 
    'disabled' => 'disabled', 'empty' => true,'options'=>$optionsList));?>
    

    $optionsList is a list of options for select box then it will preselect your particular selection.