【问题标题】:Zend Framework - Zend_Form Decorator IssueZend 框架 - Zend_Form 装饰器问题
【发布时间】:2008-12-17 22:00:02
【问题描述】:

我有一个像这样(简化)扩展 Zend_Form 的类:

class Core_Form extends Zend_Form
{
    protected static $_elementDecorators = array(
        'ViewHelper',
        'Errors',
        array('Label'),
        array('HtmlTag', array('tag' => 'li')),
    );  

    public function loadDefaultDecorators()
    {
        $this->setElementDecorators(self::$_elementDecorators);
    }
}

然后我使用该类来创建我的所有表单:

class ExampleForm extends Core_Form
{
    public function init()
    {
        // Example Field
        $example = new Zend_Form_Element_Hidden('example');
        $this->addElement($example);
    }
}

在我的一个观点中,我需要只显示这一个字段(没有其他任何由 Zend_Form 生成的字段)。所以在我看来,我有这个:

<?php echo $this->exampleForm->example; ?>

这很好用,除了它会生成这样的字段:

<li><input type="hidden" name="example" value=""></li>

这显然是因为我将元素装饰器设置为包含 HtmlTag: tag => 'li'。

我的问题是:如何禁用此元素的所有装饰器。我不需要隐藏输入元素的装饰器。

【问题讨论】:

    标签: php zend-framework zend-form


    【解决方案1】:

    设置它的最佳位置是 public function loadDefaultDecorators()

    例如这样:

    class ExampleForm extends Core_Form
        {
            public function init()
            {
                //Example Field
                $example = new Zend_Form_Element_Hidden('example');
                $this->addElement($example);
            }
    
            public function loadDefaultDecorators()
            {
                $this->example->setDecorators(array('ViewHelper'));
            }
        }
    

    【讨论】:

    • 谢谢!我不知道为什么我没有考虑重写 loadDefaultDecorators() 函数。
    【解决方案2】:

    将表单元素的装饰器重置为仅使用“ViewHelper”。例如:

    <?php echo $this->exampleForm->example->setDecorators(array('ViewHelper')) ; ?>
    

    显然,视图不是执行此操作的理想位置,但您明白了。请注意,调用 setDecorator***s***() 会重置所有装饰器,而不是添加一个新装饰器。

    【讨论】:

      【解决方案3】:

      如果您禁用隐藏元素上的 dd/dt 装饰器,您将获得无效的 XHTML,因为您将拥有在 dl 中无效的项目。唯一的解决方案是在所有表单元素上禁用这些装饰器,而不仅仅是隐藏的元素,并且也在整个表单上禁用它们。为了保持一致性,您需要在所有表单中执行此操作。

      恕我直言,这是采埃孚的错误设计决定。我的意思是,说输入的值是“术语”的“定义”在语义上是一个可爱的想法,但它并没有经过深思熟虑。

      同样的问题:Zend Framework: How do I remove the decorators on a Zend Form Hidden Element?

      【讨论】:

        【解决方案4】:

        如果你要以这种方式添加元素:

        $this->addElement(
          'text',
          'a1',
          array('required' => true, 'validators' => array('Alpha'))
        );
        

        你可以为每个元素获取dd/dt标签:

        $this->setElementDecorators(array('ViewHelper'));
        

        或者如果您打算以其他方式添加元素:

        $nombre1 = new Zend_Form_Element_Text(
                  'n1', 
                  array('id'=> 'Nombre1', 'validators' => array('Alpha') )
                    );
        //$nombre1->setDecorators(array('ViewHelper'));
        $this->addElement($nombre1);
        

        您需要取消注释:

        //$nombre1->setDecorators(array('ViewHelper'));
        

        为了禁用dd/dt 标签。 最后一种方式只是禁用当前元素,表单中的其他元素保持&lt;dd&gt;&lt;dt&gt;标签正常。

        【讨论】:

          【解决方案5】:

          这是我的工作:

          class M_Form_Element_Hidden extends Zend_Form_Element_Hidden {
             public function init() {
                $this->setDisableLoadDefaultDecorators(true);
                $this->addDecorator('ViewHelper');
                $this->removeDecorator('DtDdWrapper');
                $this->removeDecorator('HtmlTag');
                $this->removeDecorator('Label');
                return parent::init();
             }
          }
          

          然后在你的表单中,

          $element = new M_Form_Element_Hidden('myElement');
          $this->addElement($element);
          

          Source

          【讨论】:

            猜你喜欢
            • 2011-07-09
            • 1970-01-01
            • 1970-01-01
            • 2010-11-12
            • 2011-07-13
            • 1970-01-01
            • 2011-08-08
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多