2014-02-20 69 views
0

如何更改輸入表單的屬性?如何更改doctrine2中的輸入值

我用這個輸入(productform.php)創建:

$this->add(array(
       'name' => 'categoryId', 
       'attributes' => array(
         'id' => 'categoryId', 
         'type' => 'hidden', 
         'value' => '', 
       ), 
     )); 

在前面的頁面我鏈接的形式和設置的URL(....com/form/3)的特殊價值。

在indexcontroller.php中,我得到表單$form = new ProductForm();,並且想要編輯該值並從url中設置特殊值。

我的想法是$form->setAttribute('categoryId', 'value');但這不起作用。

謝謝。

indexcontroller.php

... 
     $form = new ProductForm(); 
     $form->setHydrator(new CategoryHydrator()); 
     $form->bind(new Product()); 
     $form->setAttribute('categoryId', 'value'); 
.... 

productform.php

... 

class ProductForm extends Form 
{ 
    public function __construct() 
    { 
     parent::__construct('productForm'); 
     $this->setAttribute('action', 'newproduct'); 
     $this->setAttribute('method', 'post'); 

     $this->add(array(

........ 

回答

2
$form->get('categoryId')->setValue("value"); 

更新

所以,如果你只是想填補輸入,你的意思是在HTML placeholder屬性。您可以使用setAttribute方法。

$form->get('categoryId')->setAttribute('placeholder', 'text to show'); 
+0

已更新。請具體說明您的問題。 – Bilal

0

表單視圖助手不允許在表單上設置任意的HTML屬性。這是因爲它會導致HTML無效。

如果你看看Zend\Form\Helper\AbstractHelper,有兩個屬性$validGlobalAttributes$validTagAttributes定義允許的標籤。

在表單視圖助手的情況下(Zend\Form\View\Helper\Form)的「有效的標籤屬性」將是methodaction

當你需要自定義的東西(對於JS可能);我將其更改爲data-屬性。

$form->setAttribute('data-categoryId', 'value'); 

data-a valid HTML5 attribute這是添加「域數據」 HTML元素有用,真的是「正確」的方式做你所需要的。

+0

我只想填寫輸入中的「值」。 – Peach

+0

我想我已經錯過了解你的問題; @ Bilal的回答是你需要的;雖然你可能想詳細說明'不工作'並且準確地告訴我們*什麼'不起作用'。也許用完整的控制器和表格代碼來更新你的問題。 – AlexP