1

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.

Amir
  • 1,328
  • 2
  • 13
  • 27
user2278120
  • 623
  • 2
  • 9
  • 22

1 Answers1

0

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.

Anubhav
  • 1,605
  • 4
  • 18
  • 31
  • Hey. This particular form item gets populated via ajax/json which always won't get called prior to the page loading. Is there no way to pre-populate without already having options loaded? Btw, I tried doing 'options' => array('yes' => 'yes', 'no' => 'no') just as a test and even though it had options it still didn't pre-populate, is that because one of the options wasn't CB1230 SEASHELL? – user2278120 Jan 30 '14 at 15:58
  • It is because select option need index, for example 'options' => array('yes' => 'YES', 'no' => 'NO') YES will be selected when you provide yes index. You can create options using javascript and then select your particular selection. – Anubhav Jan 30 '14 at 16:02
  • I edited in my solution, but your thing helped me come up with it, so I will mark this as the best answer. Thanks. – user2278120 Jan 30 '14 at 17:39