好的,所以我厌倦了进一步寻找......因此我只是解析了元素名以查找括号之间最深的字符串......这是我的视图助手,用于使用“form-horizontal”表单布局呈现表单的所有元素twitter bootstrap 和必填字段的星号图标(需要在 inputfilter 中设置而不是元素属性)
FormControlGroup.php(ViewHelper 将表单元素呈现为 )
<?php
namespace Application\View\Helper;
use Zend\InputFilter\InputFilter;
use Zend\Form\ElementInterface;
use Zend\Form\Element;
use Zend\Form\Fieldset;
use Zend\Form\View\Helper\AbstractHelper;
use Zend\Form\View\Helper\FormLabel;
use Zend\Form\View\Helper\FormElement;
use Zend\Form\View\Helper\FormElementErrors;
use Application\View\Helper\FormControlGroupFieldset;
use Zend\Debug\Debug;
class FormControlGroup extends AbstractHelper
{
public function __invoke(ElementInterface $elem = null,InputFilter $inputFilter = null){
if(!$elem){ return $this; }
// back up for fieldsets
if($elem instanceof Fieldset){
if($this->getView()){
$fcgf = new FormControlGroupFieldset();
$fcgf->setView($this->getView());
return $fcgf->__invoke($elem,$inputFilter);
} else {
throw new Exception\DomainException(sprintf('FormControlGroup ViewHelper expects either an Element or a Fieldset. No View Could be found in the provided Object.'));
}
}
// add control-group container
$out = '<div class="control-group">';
// add control-label class to label if label exists
$out .= $this->renderLabel($elem,$inputFilter);
// add controls container
$out .= '<div class="controls">';
// ensure renderer for used viewhelpers
if($this->getView()){
// render element
$el = new FormElement();
$el->setView($this->getView());
$out .= $el->__invoke($elem);
unset($el);
// render element errors
$er = new FormElementErrors();
$er->setView($this->getView());
$out .= $er->__invoke($elem,array('class' => 'help-inline'));
unset($er);
} else {
$out .= 'No renderer found';
}
// close containers
$out .= '</div></div>';
return $out;
}
/**
*
* @param ElementInterface $elem
* @return string rendered Label with control-label class
*/
protected function renderLabel(ElementInterface &$elem,InputFilter &$inputFilter = null){
if($elem->getLabel()){
$lblAttr = $elem->getLabelAttributes();
if($lblAttr && key_exists('class', $lblAttr)){
if(stripos($lblAttr['class'],'control-label')){
$lblAttr['class'] .= 'control-label';
}
} else {
$lblAttr['class'] = 'control-label';
}
$elem->setLabelAttributes($lblAttr);
// check whether this element is required to fill out
if($inputFilter){
$inputs = $inputFilter->getInputs();
if(key_exists($this->getRealElementName($elem),$inputs)){
if($inputs[$this->getRealElementName($elem)]->isRequired()){
$elem->setLabel($elem->getLabel().'<i class="icon-asterisk"></i>');
}
}
}
$lbl = new FormLabel();
return $lbl->__invoke($elem);
} else {
return '';
}
}
public function getRealElementName($e){
$start = strrpos($e->getName(), '[')+1;
$end = strpos($e->getName(),']');
$elName = substr($e->getName(),$start,$end-$start);
return $elName;
}
}
FormControlGroupFieldset.php(ViewHelper 将表单字段集呈现为 - 由 FormControlGroupFieldset 调用)
<?php
namespace Application\View\Helper;
use Zend\InputFilter\InputFilter;
use Zend\Form\Fieldset;
use Zend\Form\View\Helper\AbstractHelper;
use Application\View\Helper\FormControlGroup;
use Zend\Debug\Debug;
class FormControlGroupFieldset extends AbstractHelper
{
public function __invoke(Fieldset $fs=null,InputFilter $inputFilter = null){
if(!$fs){ return $this; }
$out = '<fieldset>';
if($fs->getLabel()){
$out .= '<legend>'.$fs->getLabel().'</legend>';
}
if($this->getView() && sizeof($fs->getElements()) > 0){
$cg = new FormControlGroup();
$cg->setView($this->getView());
if($inputFilter){
$inputs = $inputFilter->getInputs();
$fsInput = $inputs[$fs->getName()];
}
foreach($fs->getElements() as $e){
$out .= $cg->__invoke($e,$fsInput);
}
unset($cg);
} else {
$out .= "No Fieldset renderer found.";
}
$out .= '</fieldset>';
return $out;
}
public function getRealElementName($e){
$start = strrpos($e->getName(), '[')+1;
$end = strpos($e->getName(),']');
$elName = substr($e->getName(),$start,$end-$start);
return $elName;
}
}
您可能需要调整命名空间...