【发布时间】:2016-01-30 10:46:22
【问题描述】:
我正在尝试理解和解决整个表单集合的问题,但文档并不是很丰富,我只是不知道如何做一些我需要的特定事情。
我会参考official manual中的例子来解释我需要什么:
-
创建集合后,您可以使用自定义字段集作为目标:
$this->add(array( 'type' => 'Zend\Form\Element\Collection', 'name' => 'categories', 'options' => array( 'label' => 'Please choose categories for this product', 'count' => 2, 'should_create_template' => true, 'template_placeholder' => '__placeholder_:', **'target_element' => array( 'type' => 'Application\Form\CategoryFieldset', )**, ), ));
但是,我需要将参数传递给特定字段集的构造函数,在我的例子中是一个翻译器实例,以便能够在字段集中进行翻译。
class CategoryFieldset extends Fieldset
{
public function __construct($translator)
}
- Fieldset 的标签:如您在示例中所见,该集合输出具有相同指定标签“Category”的字段集的所有副本。相反,我需要对该标签进行编号,以根据集合的计数显示“类别 1”、“类别 2”等。这甚至可能吗?
感谢您的帮助!
【问题讨论】:
-
你解决了吗。我已经实现了您多次尝试实现的功能。但这很痛苦,我不想解释你是否解决了,因为你在 2 天前问过......
-
我有点想出类别标签部分,不确定它是否是正确的方法,但它有效:我只是在标签中放置了一个占位符,因此它们与索引一起由 JS 编号。关于翻译器,我还是没解决;我找到了许多从字段集中获取服务管理器的解决方案,但是当我将字段集生成为集合的目标类型时,它们都不起作用。
-
@PurpleHexagon 你还能帮忙翻译一下吗?
-
我可以,是否可以为表单发布更完整的代码?和翻译的具体问题?
-
所以,这是表单的相关部分:
class Contact extends Form { public function __construct($translator) { $this->add(array( 'name' => 'people', 'type' => 'collection', 'options' => array( 'label' => $translator->translate("People"), 'target_element' => array('type' => 'Application\Form\PersonFieldset'), 'count' => 2, ), )); } }这是 PersonFieldset:class PersonFieldset extends Fieldset { public function __construct($translator) { _My fields here; I need to access the translator but I can't pass it from the collection element._ } }
标签: php zend-framework2 zend-form