对所有人说早安,
我在使用ZF2时遇到了一些麻烦,我是这个框架的新手,所以请怜悯.... XDD 好吧,我想使用函数formCollection()来生成我自定义表单集合类的表单来添加包装器ul并且没关系,现在我的问题是如果我自定义formelement告诉他将元素包装在li里面,现在问题是标签仍然在li标签之外,有什么办法可以解决吗?不使用formRow()或直接编写html?
FormCollection.php
namespace Users\View\Helper;
use Zend\Form\ElementInterface;
use Zend\Form\View\Helper\FormCollection as BaseFormCollection;
class FormCollection extends BaseFormCollection {
public function render(ElementInterface $element) {
return '<ul>'. parent::render($element).'</ul>;
}
}
FormElement.php
namespace Users\View\Helper;
use Zend\Form\ElementInterface;
use Zend\Form\View\Helper\FormElement as BaseFormElement;
class FormElement extends BaseFormElement {
public function render(ElementInterface $element) {
return '<li>'. parent::render($element).'</li>';
}
}
结果HTML
<form name="Register" method="post" action="/">
<ul>
<label for="name">Full Name</label>
<li>
<input type="text" value="" name="name">
</li>
<label for="password">Password</label>
<li>
<input type="password" value="" required="required" name="password">
</li>
</ul>
它确实让我疯狂,可能很容易解决T_T
感谢。
答案 0 :(得分:2)
对于<li><label></label><input></li>
,您必须创建新的帮助器FormLabel.php:
namespace Users\View\Helper;
use Zend\Form\View\Helper\FormLabel as BaseFormLabel;
class FormLabel extends BaseFormLabel{
public function openTag($attributesOrElement = null){
return '<li>'.parent::openTag($attributesOrElement);
}
}
并更新FormElement.php以正确地换行提交:
namespace Users\View\Helper;
use Zend\Form\ElementInterface;
use Zend\Form\View\Helper\FormElement as BaseFormElement;
class FormElement extends BaseFormElement{
public function render(ElementInterface $element){
if($element->getAttribute('type') == 'submit'){
return '<li>'.parent::render($element).'</li>';
}else{
return parent::render($element).'</li>';
}
}
}
对于<li><label></label></li><li><input></li>
,您只需创建FormLabel.php:
namespace Users\View\Helper;
use Zend\Form\View\Helper\FormLabel as BaseFormLabel;
class FormLabel extends BaseFormLabel{
public function openTag($attributesOrElement = null){
return '<li>'.parent::openTag($attributesOrElement);
}
public function closeTag(){
return '</label></li>';
}
}
没有你的任何更新FormElement.php
像这样的一些想法... =)希望它有所帮助。答案 1 :(得分:1)
对于那些像我一样被困的人我定制了帮助者FormCollection(不知道是否有更好的方法来做X /)
将此课程置于
之下&LT;模块名称&gt; / src /&lt;模块名称&gt; /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;
}
}
然后转到&lt;模块名称&gt; /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