是否可以在由集合组成的字段集上设置id?我有几个实体(用户、地址、附件等),在我的user-entity中,有几个字段集是由一个集合组成的。因此一个用户可以有多个地址或附件。我的收藏是这样建立的:
$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。请帮帮忙
发布于 2013-10-04 22:00:21
一种可能的方法是扩展Zend\Form\View\Helper\FormCollection,并将扩展注册为默认formCollection帮助器的替代。
出于本例的目的,我将假定您的模块名为Application__。
首先,您需要定义一个自定义类来扩展前面提到的Zend helper。对于我的扩展/覆盖,我通常保持与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_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时检查是否有自定义属性,如果发现了就追加它们。为此,我们将修改121到125行,使其如下所示:
$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">!
希望这能有所帮助!
发布于 2013-05-08 00:15:48
只需将其添加到您的元素的attributes下:
$this->add(array(
'attributes' => array(
'id' => 'someId',
'onclick' => 'return add_address()';
),
// your other stuff, type, name, options, etc...
));发布于 2014-03-21 20:34:49
不知道为什么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);
}
}https://stackoverflow.com/questions/16419547
复制相似问题