2013-05-07 162 views
2

是否可以在由集合創建的字段集中設置id?我有幾個實體(用戶,地址,附件等),在我的用戶實體中,我有幾個使用集合創建的字段集。所以用戶可以有多個地址或附件。我的收藏品是這樣構建的:如何在集合字段集(doctrine 2,zend framework 2)上設置屬性(id)

$addressFieldset = new AddressFieldset($serviceManager); 

$this->add(array(
    'type' => 'Zend\Form\Element\Collection', 
    'name' => 'addresses', 
    'options' => array(
     'label' => 'Choose address for user', 
     'count' => 1, 
     'should_create_template' => true, 
     //'template_placeholder' => '__placeholder__', 
     'allow_add' => true, 
     'target_element' => $addressFieldset 
     ), 
    )); 

$this->add(array(
    'name' => 'addAddress', 
    'type' => 'button', 
    'options' => array('label' => 'Add Address', 
      ), 
    )); 
$this->get('addAddress')->setAttribute('onclick', 'return add_address()'); 

我的問題是我在我的userfieldset中有多個集合。所以,當我要動態地添加一些地址(例如,我使用:http://zf2.readthedocs.org/en/latest/modules/zend.form.collections.html)中,例如有以下的javascript:

function add_address() { 
    var currentCount = $('form > fieldset > fieldset').length; 
    var template = $('form > fieldset > span').data('template'); 

    template = template.replace(/__index__/g, currentCount); 

    $('form > fieldset').append(template); 

    return false; 
} 

但是,我的問題是,如果我使用的例子,它增加了addressfieldsets也附件下。我想是這樣的:

function add_address() { 
    var currentCount = $('form > #addressFieldset > fieldset').length; 
    var template = $('form > #addressFieldset > span').data('template'); 

    template = template.replace(/__index__/g, currentCount); 

    $('form > #addressFieldset').append(template); 

    return false; 
} 

有了這個,我只能訪問addressFieldset,但我怎麼能在AddressFieldset設置一個ID? 我的樹應該是這樣的:

<form> 
    <fieldset id="addressFieldset"> 
     <legend>Choose addresses</legend> 
     <fieldset> 
      //Data 
     </fieldset> 
    </fieldset> 
    <fieldset id="attachmentFieldset"> 
     <legend>Choose attachments</legend> 
     <fieldset> 
      //Data 
     </fieldset> 
    </fieldset> 
</form> 

我不知道如何設置的ID在字段集。請幫助

回答

1

只需添加它下attributes您的元素:

$this->add(array(
    'attributes' => array(
     'id' => 'someId', 
     'onclick' => 'return add_address()'; 
    ), 
    // your other stuff, type, name, options, etc... 
)); 
+1

我在我的視圖中創建了帶id的字段集。在zend中,現在需要在表單集合中設置一個id,因爲該viewhelper只在字段集本身設置了圖例。所以在字段集上沒有任何屬性。當然,它有助於謝謝你 – Haidy 2013-05-08 14:34:50

2

一個這樣做是延長Zend\Form\View\Helper\FormCollection和註冊擴展爲默認formCollection幫手替換的可能途徑。

爲了本示例的目的,我假設您的模塊名爲Application

首先,您需要定義一個自定義類來擴展上述的Zend幫助程序。對於我的擴展/覆蓋,我通常保持與Zend相同的目錄結構,但我將「Zend」替換爲我的模塊的名稱,所以在我的情況下,該類將駐留在module/Application/src/Application/Form/View/Helper/FormCollection.php中。

一旦這樣做了,你可以通過添加如下行到module/Application/config/module.config.php文件替換幫手:

'view_helpers' => array(
    'invokables' => array(
     'formCollection' => 'Application\Form\View\Helper\FormCollection', 
    ), 
), 

這告訴Zend的,每當一個名爲formCollection視圖助手被調用,使用這個類來代替默認一個。

接下來,您需要覆蓋render方法。不幸的是,您必須將原始方法的內容複製/粘貼到您的課程中(因爲fieldset標記的代碼是硬編碼的),但如果您需要該功能,則需付出很小的代價。

在返回結果之前,您需要修改在變量中包含$markup變量的位。

在我們這樣做之前,我們需要在工廠數組中定義fieldset的屬性。我決定把我的字段集屬性在名爲fieldset_attributes這樣的關鍵:

$this->add(array(
    'type' => 'Collection', 
    'name' => 'addresses', 
    'options' => array(
     'label'     => 'Choose address for user', 
     'count'     => 1, 
     'allow_add'    => true, 
     'allow_remove'   => true, 
     'create_new_objects'  => true, 
     'should_create_template' => true, 
     'target_element'   => $addressFieldset, 
     'attributes'    => array(
      'id' => 'addressFieldset', 
     ), 
    ), 
)); 

當然,你可以,如果你想不同的命名鍵或重組的陣列,這純粹取決於你。只要確保您更新了我們即將相應修改的render方法。

因此,最後要做的就是在包裝$markup時檢查自定義屬性,並追加它們(如果有的話)。爲此,我們將修改行121125看起來像這樣:

$attributeString = ''; 
$options = $element->getOptions(); 
if (!empty($options['attributes'])) { 
    $attributeString = ' ' . $this->createAttributesString($options['attributes']); 
} 

$markup = sprintf(
    '<fieldset%s><legend>%s</legend>%s</fieldset>', 
    $attributeString, 
    $label, 
    $markup 
); 

如果你現在刷新頁面,你會看到<fieldset>已更改爲<fieldset id="addressFieldset">

希望有幫助!

+0

! – 2015-01-21 05:40:31

0

不知道爲什麼ZF1的表單裝飾已在ZF2被刪除...

IMO裝飾表單元素的最有效的方法是「否決」 ZF2的表格視圖助手......即使我做得相當野蠻...

<?php 
    namespace Application\Helpers\View; 

    use Zend\Form\ElementInterface; 
    use Zend\Form\View\Helper\FormCollection as BaseFormCollection; 

    class FormCollection extends BaseFormCollection 
    { 
     public function render(ElementInterface $element) 
     { 
      if($element->getAttribute('name') == 'usertype') 
       return str_replace('<fieldset>', '<fieldset class="noborder">', parent::render($element)); 
      else 
       return parent::render($element); 
     } 
    }