0

大家早上好,

我在使用 ZF2 时遇到了一些问题,我是这个框架的新手,所以请怜悯...... XDD 好吧,我想使用函数 formCollection() 来生成表单我自定义了表单集合类以添加包装器 ul没关系,现在我的问题是,如果我自定义 formelement 告诉他将元素包装在 li 中,现在的问题是标签仍然在 li 标签之外,有什么办法解决吗?不使用 formRow() 或直接编写html?

表单集合.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>;
    }

}

表单元素.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

谢谢。

4

2 回答 2

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 中没有任何更新

一些这样的想法... =)希望它有所帮助。

于 2013-12-18T20:29:56.683 回答
1

对于那些像我一样被卡住的人,我定制了助手 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

于 2013-09-08T13:28:10.413 回答