对于那些像我一样被卡住的人,我定制了助手 FormCollection(不知道是否有更好的方法来做 X/)
把这个类放在
<模块名称> /src/ <模块名称>/View/Helper/FormCollection.php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @CustomizedBy Fabio Tagliabue
* @UpdaterWS http://www.polarfoxlab.com
*/
namespace Users\View\Helper;
use Zend\Form\ElementInterface;
use Zend\Form\View\Helper\FormCollection as BaseFormCollection;
class FormCollection extends BaseFormCollection {
public function __invoke(ElementInterface $element = null, $wrap = true,$ExternalWrapper = '',$RowWrapper = 'div')
{
if (!$element) {
return $this;
}
$this->setShouldWrap($wrap);
return $this->render($element,$ExternalWrapper,$RowWrapper);
}
public function render(ElementInterface $element,$ExternalWrapper,$RowWrapper)
{
$renderer = $this->getView();
if (!method_exists($renderer, 'plugin')) {
// Bail early if renderer is not pluggable
return '';
}
$markup = '';
$templateMarkup = '';
$escapeHtmlHelper = $this->getEscapeHtmlHelper();
$elementHelper = $this->getElementHelper();
$fieldsetHelper = $this->getFieldsetHelper();
if ($element instanceof CollectionElement && $element->shouldCreateTemplate()) {
$templateMarkup = $this->renderTemplate($element);
}
$markup .= $this->PrintWrapper($ExternalWrapper,true);
foreach ($element->getIterator() as $elementOrFieldset) {
$markup .= $this->PrintWrapper($RowWrapper,true);
if ($elementOrFieldset instanceof FieldsetInterface) {
$markup .= $fieldsetHelper($elementOrFieldset);
} elseif ($elementOrFieldset instanceof ElementInterface) {
$markup .= $elementHelper($elementOrFieldset);
}
$markup .= $this->PrintWrapper($RowWrapper,false);
}
$markup .= $this->PrintWrapper($ExternalWrapper,false);
// If $templateMarkup is not empty, use it for simplify adding new element in JavaScript
if (!empty($templateMarkup)) {
$markup .= $templateMarkup;
}
// Every collection is wrapped by a fieldset if needed
if ($this->shouldWrap) {
$label = $element->getLabel();
if (!empty($label)) {
if (null !== ($translator = $this->getTranslator())) {
$label = $translator->translate(
$label, $this->getTranslatorTextDomain()
);
}
$label = $escapeHtmlHelper($label);
$markup = sprintf(
'<fieldset><legend>%s</legend>%s</fieldset>',
$label,
$markup
);
}
}
return $markup;
}
/*
* @param array wrapper keys are attribute + type is html tag
* @param bool open if need to open or close tag
*/
private function PrintWrapper($wrapper,$open=true)
{
$tag='';
if(!empty($wrapper))
{
if($open) $tag='<';
else $tag="</";
foreach($wrapper as $attribute => $value)
{
if(strtolower($attribute)=="type")
$tag.="{$value}";
else
$tag.=" {$attribute}='{$value}'";
}
$tag.='>';
}
return $tag;
}
}
然后转到 <模块名称> /Module.php
并添加这个
public function getViewHelperConfig() {
return array(
'invokables' => array(
'FormCollection' => '< module name >\View\Helper\FormCollection',
)
);
}
现在在表单页面调用中使用它
echo $this->formCollection($form,true,'',array('type'=>'div','class'=>'form-group'));
第三个参数是 ExternalWrapper,第四个是 RowWrapper 只需设置要使用的标签的类型,然后设置要分配的其他属性:D
希望这可以帮助某人:D