2014-01-30 28 views
1

我正在使用Cake的表单助手,它应该预先填充,当我设置$ this-> request-> data到我的控制器中的某个东西。它是为普通的type =“text”输入框预填充的,但不适用于type =“select”。有人知道为什么CakePHP常规输入框预填充但不是选择框?

如果我在我看来,PR($这个 - >请求 - >数据)我得到这样的结果:

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

但这

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

我试着去掉'empty'=> true并移除占位符并删除残疾人,但没有任何东西让diff erence。

任何想法家伙?谢谢。

编辑:

我刚刚结束了使用此。

      <?=$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'] : ''))));?> 

我失去了empty => true功能和禁用的功能,但我会通过javascript控制这些功能。

谢谢。

回答

0

你必须提供选项

<?=$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是对选择框,然后将预选的特定选择的选项列表。

+0

嘿。这个特定的表单项通过ajax/json填充,它们在页面加载之前总是不会被调用。如果没有加载选项,是否无法预先填充?顺便说一句,我试着做'选择'=>数组('是'=>'是','否'=>'不')只是作为一个测试,即使它有选项,它仍然没有预先填充,是那是因为其中一个选项不是CB1230 SEASHELL? – user2278120

+0

这是因为select选项需要索引,例如'options'=> array('yes'=>'YES','no'=>'NO')当您提供yes索引时,会选择YES。您可以使用JavaScript创建选项,然后选择您的特定选择。 – Anubhav

+0

我在我的解决方案中进行了编辑,但是您的工作让我想起了它,所以我会将其标记为最佳答案。谢谢。 – user2278120