2013-05-07 164 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); 
     } 
    }