【问题标题】:zf2 Zend 2 formCollection / FormElement Helper classzf2 Zend 2 formCollection / FormElement Helper 类
【发布时间】:2013-09-08 11:53:21
【问题描述】:

大家早上好,

我在使用 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

谢谢。

【问题讨论】:

    标签: php forms zend-framework2 formcollection


    【解决方案1】:

    对于&lt;li&gt;&lt;label&gt;&lt;/label&gt;&lt;input&gt;&lt;/li&gt;,您必须创建新的助手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>';
            }
        }
    } 
    

    对于&lt;li&gt;&lt;label&gt;&lt;/label&gt;&lt;/li&gt;&lt;li&gt;&lt;input&gt;&lt;/li&gt;,您只需创建 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 中没有任何更新

    这样想... =) 希望对您有所帮助。

    【讨论】:

    • 感谢您的回答....基本上是我在了解更好的 ZF2 后所做的:D 但仍然比我的第一个解决方案更好,所以我切换了批准的解决方案:D
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2012-09-20
      • 1970-01-01
      • 1970-01-01
      • 2012-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-20
      • 1970-01-01
      相关资源
      最近更新 更多