【问题标题】:Zend_Form renders all fields as textZend_Form 将所有字段呈现为文本
【发布时间】:2013-01-11 08:26:50
【问题描述】:

我有一个 Zend_Form 表单,带有一些自定义装饰器,如下所示:

$decorators = array();
$decorators[] = new Zend_Form_Decorator_ViewHelper(array());
$decorators[] = new Zend_Form_Decorator_Errors;
$decorators[] = new Zend_Form_Decorator_HtmlTag(array('tag' => 'div', 'class' => 'form-item'));
$decorators[] = new Zend_Form_Decorator_Label(array('class' => 'form-label'));
$decorators[] = new Zend_Form_Decorator_Callback(array(
    'callback' => function($content, $element, $options) {
        return sprintf('<div class="form-row">%s</div>', $content);
    },
    'placement' => false
));
$this->setElementDecorators($decorators);

问题是,所有字段都呈现为文本输入。为什么会这样?

编辑:我发现,它不一定将所有输入呈现为文本输入,而是使用表单中第一个输入的类型呈现它们。这是我使用的表单示例(装饰器设置为 int parent's init):

<?php

class Form_Users_Add extends Form_Base {
    protected $pbxs = array(1 => 'Element 1', 2 => 'Element 2');

    public function init() {
        $monitors = new Zend_Form_Element_Checkbox('prefered_screen_count');
        $monitors->setCheckedValue(2);
        $monitors->setUncheckedValue(1);
        $monitors->setLabel('two_monitors');
        $this->addElement($monitors);

        $pbx = new Zend_Form_Element_Select('asterisk_id');
        $pbx->setMultiOptions($this->pbxs);
        $pbx->setLabel('users_asterisk_id');
        $this->addElement($pbx);

        parent::init();
    }

}

【问题讨论】:

  • 可以显示表单代码吗?作为一个附带问题:为什么要为行使用Callback 装饰器?为什么不使用HtmlTag,就像您对项目本身所做的那样?只是好奇。
  • 我编辑了这个问题。而且我正在使用回调,因为我无法让两个 HtmlTag 工作 - 可能我做错了什么,但它们刚刚被替换。
  • 啊啊,在一个元素上使用同一个装饰器类的多个实例有一个技巧:你需要给装饰器名称起别名。例如:array(array('my-item' =&gt; 'HtmlTag'), array('tag' =&gt; 'div', 'class' =&gt; 'some-class')),。然后在行的 HtmlTag 装饰器上使用不同的别名 - 比如 my-row

标签: zend-framework zend-form


【解决方案1】:

耶!我已经解决了这个问题!原因是我使用了类的实例,而不是名称。这样每个元素都使用同一个装饰器实例。

【讨论】:

    猜你喜欢
    • 2011-04-30
    • 2011-08-31
    • 1970-01-01
    • 1970-01-01
    • 2017-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多