0

我已经创建了一个标准应用程序,它使用集合来映射一对多关系(不使用 Doctrine),就像它在http://framework.zend.com/manual/2.2/en/modules/zend.form中描述的那样.collections.html我有类似下面的代码:

class Person {
  protected $attrributes;
} 

class Attribute {
  protected $attr1;
  protected $attr2;
}

我已经创建了AttributeFieldset和 所需的匹配AttributeForm项,并AttributeFieldset在我的PersonForm:

$this->add(
  array(
    'type' => 'Zend\Form\Element\Collection',
    'name' => 'attributes',
    'options' => array(
      'label' => _("Add person attribute"),
      'count' => 1,
      'should_create_template' => true,
      'allow_add' => true,
      'target_element' => array(
        'type' => 'Persons\Form\AttributesFormFieldset'
      )
    )
  ));

调用$this->formCollection()视图助手将为集合和数据模板生成默认 HTML,以通过文档中指定的 javascript 动态添加新属性。

但是,我想要完成的是列出一个包含人员所有现有属性的表格,并带有一个编辑/删除选项,并创建一个带有集合字段集的模式窗口以向该人员添加新属性。

想象一下下面的html:

<a href="#" onclick="add(this); return false;">Add new attribute</a>
<table>
  <? foreach( $this->person->attributes as $attribute ): ?>
    <tr>
      <td><?= $attribute['attr1']; ?></td>
      <td>
        <a href="#" onclick="edit(this); return false">Edit</a> | <a href="#" onclick="delete(this); return false">Delete</a>
      </td>
    </tr>
  <? endforeach; ?>
</table>

我知道我可以完全跳过 formCollection 并<input type="hidden">按照 ZF2 Collection 期望它们(例如attribute[0][attr1]等)添加到表的每一行上的方式添加标签,并动态创建表单,但我猜我会错过 ZF2 InputFilters .

有没有更好的方法来实现这一点?以前有人做过吗?

4

1 回答 1

1

使用集合并需要自定义标记很烦人。但这并不难:

$collections = $form->get('collection-element');
echo '<table>'
// render thead and tbody open if needed
foreach ($collections as $col) : ?>
    <tr>
        <td><?php echo $this->formInput($col->get('input-from-collection-name'); ?></td>
        <td><?php echo $this->formInput($col->get('other-input-from-collection-name'); ?></td>
    </tr>
<?php endforeach;
echo '</table>';

它是如此简单。这很烦人。此外,通过配置中的默认示例:template为其他元素添加 a 的最简单方法是只呈现一次表单,然后复制生成的 HTML,然后将其粘贴到您的数据模板中。

于 2013-09-28T18:21:32.080 回答