【问题标题】:How to pass an argument to a Zend Form Collection instance & How to set custom Fieldset labels in ZF2?如何将参数传递给 Zend 表单集合实例以及如何在 ZF2 中设置自定义字段集标签?
【发布时间】:2016-01-30 10:46:22
【问题描述】:

我正在尝试理解和解决整个表单集合的问题,但文档并不是很丰富,我只是不知道如何做一些我需要的特定事情。

我会参考official manual中的例子来解释我需要什么:

  1. 创建集合后,您可以使用自定义字段集作为目标:

    $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) }

  1. 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


【解决方案1】:

我检查了集合的来源。 Collection 只是克隆 target_element。我的解决方案很简单且有效:

class CategoryFieldset extends Fieldset implements InputFilterProviderInterface
{
    static $lp = 1;// <-----------  add this line

    public function __clone() //<------------ add this method
    {
        parent::__clone(); 
        $oldLabel = $this->elements['name']->getLabel();
        $this->elements['name']->setLabel($oldLabel . ' ' . self::$lp++);
    }

【讨论】:

    【解决方案2】:

    首先,不要将翻译器传递给 Fieldset,而是在 Fieldset 之外使用翻译器。首先从表单中获取值,翻译它们,然后将它们设置回表单。好处是您将表单和翻译逻辑分开。

    第二个,使用$form-&gt;prepare(),然后遍历Collection

    $form->prepare(); //clones collection elements
    
    $collection = $form->get('YOUR_COLLECTION_ELEMENT_NAME');
    foreach ($collection as $fieldset)
        $fieldset->get('INDIVIDUAL_ELEMENT_NAME')->setLabel("WHATEVER YOU WANT");
    

    示例:

    /*
     * In your model or controller:
     */
    $form->prepare();
    
    $collection = $form->get('categories');
    foreach ($collection as $fieldset)
    {
        $label = $fieldset->get('name')->getLabel();
        $translatedLabel = $translator->translate($label);
        $fieldset->get('name')->setLabel($translatedLabel);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 2012-08-12
      • 1970-01-01
      • 2012-07-24
      • 1970-01-01
      • 1970-01-01
      • 2011-09-12
      相关资源
      最近更新 更多