Zend:如何在使用 addElement() 作为工厂时向表单元素添加装饰器?
正如标题所暗示的,我有一个关于 Zend 框架中的装饰器的问题。请考虑以下代码示例:
$this->addElement('select', 'DisplayUntil', array(
'label' => 'Display until:',
'multiOptions' => $this->getOptions(),
'filters' => array(
'HTMLEntities',
'StringTrim',
),
'validators' => array(
'Int',
),
));
这是我向表单添加元素的首选方法。在尝试建立最佳实践时,我标准化了使用工厂方法而不是方法链。为什么?我个人发现它更具可读性,并且它缩短了我的自定义表单类的长度,即更少的编码。上面的示例工作得很好,但是,我很难找到使用相同方法添加装饰器的文档或示例。因此,使用相同的代码片段,我假设在添加装饰器时您将执行以下操作:
$this->addElement('select', 'DisplayUntil', array(
'label' => 'Display until:',
'multiOptions' => $this->getOptions(),
'filters' => array(
'HTMLEntities',
'StringTrim',
),
'validators' => array(
'Int',
),
'decorators' => array(
'ViewHelper',
'Label' => array(
'tag' => 'dt'
),
'HtmlTag' => array(
'tag' => 'div',
'openOnly' => true,
'id' => 'divDisplayUntil',
'placement' => 'prepend',
),
),
));
不幸的是,我收到“在注册表中未找到名为 'Dt' 的插件;使用的路径:Zend_Form_Decorator_: Zend/Form/Decorator/”错误消息。我对 Zend 相当陌生,如果有人能向我展示如何使前面的代码示例工作,我将不胜感激。这种方法可能吗?
谢谢你!
As the title implies, I have a question concerning decorators in the Zend Framework. Please consider the following code example:
$this->addElement('select', 'DisplayUntil', array(
'label' => 'Display until:',
'multiOptions' => $this->getOptions(),
'filters' => array(
'HTMLEntities',
'StringTrim',
),
'validators' => array(
'Int',
),
));
This is my preferred method for adding elements to a form. In attempting to establish best practices, I have standardized on using factory methods rather than method chaining. Why? I personally find it much more readable and it shortens the length of my custom form class, i.e. less coding. The above example works beautifully, however, I am having a hard time locating documentation or examples where the same method is used for adding decorators. So using the same code snippet, I assumed when adding decorators you would do the following:
$this->addElement('select', 'DisplayUntil', array(
'label' => 'Display until:',
'multiOptions' => $this->getOptions(),
'filters' => array(
'HTMLEntities',
'StringTrim',
),
'validators' => array(
'Int',
),
'decorators' => array(
'ViewHelper',
'Label' => array(
'tag' => 'dt'
),
'HtmlTag' => array(
'tag' => 'div',
'openOnly' => true,
'id' => 'divDisplayUntil',
'placement' => 'prepend',
),
),
));
Unfortunately I receive a "Plugin by name 'Dt' was not found in the registry; used paths: Zend_Form_Decorator_: Zend/Form/Decorator/" error message. I am fairly new to Zend and I would appreciate it if someone could show me how to make the preceding code example work? Is this method even possible?
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
装饰器似乎多了一层数组。这是来自 1.1x 手册:
所以在你的情况下尝试
Decorators seem to have one more layer of arrays. This is from the 1.1x manual:
So in your case try