如何在 ZF2 中将输入元素包装在 div 内

How to wrap input element inside div in ZF2

我正在关注 this link

看来我有

echo $this->formRow($product->get('name'));

并且在源代码中显示为

<label>Name of the product<input name="product[name]" required="required" type="text" value=""></label>

但我想要这样

<div><label>Name of the product</label></div>
<div><input name="product[name]" required="required" type="text" value=""></div>

也许这个问题在某个地方问过,但我找不到。

我正在使用 php 5.6

编辑:

根据 this link 中的文档,我能够解决这个 issue.Following 的问题。我在这部分有问题

echo $this->formCollection($product->get('categories'));

我这样试过

echo "<div>".$this->formLabel($this->get('categories'))."</div>";
echo "<div>".$this->formInput($this->get('categories'))."</div>";

但是它抛出致命错误。

Catchable fatal error: Object of class Zend\Form\View\Helper\FormLabel could not be converted to string in /opt/lampp/htdocs/zend2/module/Test/view/test/index/testform.phtml on line 39

我该如何解决这个问题?

而不是$this->formRow,您可以使用下面的代码,可以将标签和输入分开。

echo $this->formLabel($form->get('name'));
echo $this->formInput($form->get('name'));

它会输出这个:

<label>Name of the product</label>
<input name="product[name]" required="required" type="text" value="">

然后就可以在echo中使用div标签了。所以这就是你想要的:

echo "<div>".$this->formLabel($form->get('name'))."</div>";
echo "<div>".$this->formInput($form->get('name'))."</div>";