0

在我當前的項目中,我使用了嵌套的Zend\Form\Fieldset s和Zend\Form\Collections,它們提供了一種非常舒適的方式將複雜的對象結構映射到表單,以便從表單輸入中獲取完整的對象(可以保存)。如何裝飾ZF2集合中的字段集的元素?

的問題:我有一個FieldsetFooFieldset含有Elementfoo_elementLabel爲「foo元件」(代碼見下文),並且需要使用兩次:1.作爲單個Fieldset; 2.在Collection。在表單的第一位,我希望顯示其元素;在第二個地方,我想禁用標籤(或者可能改變它們)。 (我也想把它格式化在第二種情況中的另一種方式,但現在最重要的事情是標籤。)

根據上下文如何裝點Zend\Form\ElementZend\Form\Fieldset的在Zend\Form\Element\Collection


代碼

class FooFieldset extends Fieldset implements InputFilterProviderInterface 
{ 
    public function init() 
    { 
     $this->add([ 
      'type' => 'text', 
      'name' => foo_element', 
      'options' => ['label' => _('foo element')] 
     ]); 
    } 
    public function getInputFilterSpecification() { ... } 
} 

class BarFieldset extends Fieldset implements InputFilterProviderInterface 
{ 
    public function init() 
    { 
     $this->add([ 
      'name' => 'foo', 
      'type' => 'My\Form\Fieldset\Foo', 
      'options' => [] 
     ]); 
    } 
    public function getInputFilterSpecification() { ... } 
} 

class BuzFieldset extends Fieldset implements InputFilterProviderInterface 
{ 
     $this->add(
      [ 
       'name' => 'foos', 
       'type' => 'Zend\Form\Element\Collection', 
       'options' => [ 
        'label' => _('multiple foos'), 
        'count' => 5, 
        'should_create_template' => true, 
        'template_placeholder' => '__placeholder__', 
        'allow_add' => true, 
        'target_element' => [ 
         'type' => 'Order\Form\Fieldset\Foo', 
        ], 
        'label_attributes' => [ 
         'class' => 'col-md-12' 
        ] 
       ] 
      ]); 
    public function getInputFilterSpecification() { ... } 
} 

echo $this->formRow($myForm->get('main_fieldset')->get('bar')->get('foo')->get('foo_element'); 
echo $this->formRow($myForm->get('main_fieldset')->get('buz')->get('foos'); 

解決方法1

這將有可能使用另一種Fieldset,例如一個FooFieldst(sometnig like FooFieldsetForUsingInCollection extends FooFieldst)的子類,並在那裏調整Label(和其他設置)。

解決方法2

也將是可以訪問在視圖腳本CollectionElement秒和操縱它們在那裏(如here證實)。但我不太喜歡這個解決方案,因爲Fieldset被定義在多個地方。如果Collection元素的數量是可變的,那麼還需要進一步的努力。

+0

'main_fieldset' ='BuzFieldset'? – AlexP

+0

不,'Bar'和'Buz'都是'MainFieldset'的Sub-'Fieldset'。對不起,這是一個錯字。請參閱更正的代碼。 – automatix

回答

0

看起來您需要在自己的字段集中重用'foos'集合和'bar'元素,同時保持當前創建的方式。

我會

  • 移動集合元素fooBuzFieldset::init,進入它自己的工廠(創建於工廠的元素和它所有的選項)。

  • 將表單元素管理器註冊爲新服務​​,可以將其稱爲FooCollection。這個元素現在可重用,可以從表單元素管理器調用$fem->get('FooCollection')

  • 替換刪除$fieldset->add('type' => 'Zend\Form\Element\Collection')$fieldset->add('type' => 'FooCollection')BuzFieldset

  • 對於foo_element重複以新的服務名稱FooElement

  • 然後,您需要創建一個名爲FooCollectionAndFooElementFieldsetFactory的新字段集工廠,該工廠將返回一個新字段集,並附上FooCollectionFooElement

  • 工廠main_fieldset決定是否需要附加FooCollectionAndFooElementFieldsetFactory或現有barbaz字段集。

+0

非常感謝您的回答!雖然,這似乎有點太複雜。我認爲,目前還沒有針對這個問題的Zend解決方案。但是,如果我必須解決它,我會用這種方式:1.創建一個'FooForCollectionFieldsetFactory'; 2.在那裏操作'FooFieldset'; 3.將'My \ Form \ Fieldset \ FooForCollection'=>'My \ Form \ Fieldset \ Factory \ FooForCollectionFieldsetFactory'添加到'module.config.php'中的'form_elements.factories'; 4.將'Collection'的'target_element.type'設置爲''My \ Form \ Fieldset \ FooForCollection''。 – automatix

相關問題