3

我需要一个简单的工作示例,如何在 Zend Form 中包含一个集合元素,我已经从 Zend Framework 2站点和 StackOverflow 中的先前帖子中看到了一些示例,其中大多数都指向此链接。但是现在我没有使用 Fieldsets 并继续使用 Forms,所以如果有人能以正确的方式指导我,当用户获得一个用户可以从显示的页面中选择多个选项时,我如何包含一个简单的集合元素收集表格。从数据库中填充收集表单会更好。

我已经在互联网上搜索了很长一段时间,并认为我会在这里发布,以便 Zend profis 可以提供他们的建议。

仅供参考:通常可以以这种方式在 Zend Form 中包含一个静态下拉框

$this->add(
    array(
        'name'       => "countr",
        'type'       => 'Zend\Form\Element\Select',
        'options'    => array(
            'label' => "Countries",
            'options' => array(
                'country1' => 'Brazil',
                'country2' => 'USA',
                'country3' => 'Mexico',
                'country4' => 'France',
            )
        )
    )
);

所以我期待一个简单的例子,它可以让我基本了解如何做到这一点。

4

2 回答 2

1

老实说,我在这里看不到你的问题。由于表单集合Fieldsetextends which extends Element,您可以将其作为常规元素添加到表单中。视图助手将递归处理渲染。

第 1 步:创建表单集合(创建 的实例Zend\Form\Element\Collection)。如果必须以某种方式动态添加元素,我会为此创建一个工厂类。

第 2 步:将其添加到表单中。(例如使用$form->add($myCollectionInstance).)

第 3 步:渲染它。Zend\Form\View\Helper\Collection是一个很好的视图助手,可以毫无痛苦地渲染整个表单。

您还可以创建一个新类扩展Zend\Form\Element\Collection并使用构造函数添加您需要的字段。因此,您可以使用粘贴在问题中的数组将其添加到表单中。此外,您可以直接在注释中使用它。

希望这可以帮助。

于 2012-09-27T14:47:38.413 回答
0

如果您只想用选项值填充选择列表,您可以将数组添加到控制器中的选择列表中:

$form = new MyForm();
$form->get('countr')->setOptions(array('value_options'=>array(
                    'country1' => 'Brazil',
                    'country2' => 'USA',
                    'country3' => 'Mexico',
                    'country4' => 'France',
                ));

该数组可以从数据库中获取。

这是以最简单的方式使用表单集合的另一个示例。在此示例中,它在集合中创建输入文本元素并填充它们。元素的数量取决于数组:

class MyForm extends \Zend\Form\Form
{
    $this->add(array(
        'type' => '\Zend\Form\Element\Collection',
        'name' => 'myCollection',
        'options' => array(
            'label' => 'My collection',
            'allow_add' => true,
    )
    ));
}

    class IndexController extends AbstractActionController
    {

    public function indexAction
    {
        $form = new MyForm();

        $this->addElementsFromArray($form, array(

                'country1' => 'Brazil',
                'country2' => 'USA',
                'country3' => 'Mexico',
                'country4' => 'France',
            ));

    //the above line can be replaced if fetching the array from a db table:
    //$arrayFromDb = getArrayFromDb(); 
    //$this->addElementsFromArray($form, $arrayFromDb);

    return array(
        'form' => $form
    );
    }

    private function addElementsFromArray($form, $array)
    {
        foreach ($array as $key=>$value)
        {
        $form->get('myCollection')->add(array(
                //'type' => '\Zend\Form\Element\SomeElement',
                'name' => $key,
                'options' => array(
                        'label' => $key,
                ),
                'attributes' => array(
                        'value' => $value,
                )
        ));
        }
    }
}

索引.phtml:

$form->setAttribute('action', $this->url('home'))
     ->prepare();
echo $this->form()->openTag($form);
echo $this->formCollection($form->get('myCollection'));
echo $this->form()->closeTag();
于 2013-08-22T18:16:56.057 回答