Zend:如何在使用 addElement() 作为工厂时向表单元素添加装饰器?

发布于 2024-12-31 23:05:35 字数 1290 浏览 2 评论 0原文

正如标题所暗示的,我有一个关于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧话新听 2025-01-07 23:05:35

装饰器似乎多了一层数组。这是来自 1.1x 手册:

$this->addDecorators(array(
    array('ViewHelper'),
    array('Errors'),
    array('Description', array('tag' => 'p', 'class' => 'description')),
    array('HtmlTag', array('tag' => 'dd')),
    array('Label', array('tag' => 'dt')),
));

所以在你的情况下尝试

'decorators' => array(
    array('ViewHelper'), 
    array('Label', array(
        'tag' => 'dt'
        )
    ),
    array('HtmlTag', array(
        'tag' => 'div',
        'openOnly' => true,
        'id' => 'divDisplayUntil',
        'placement' => 'prepend',
        )
    ),
),

Decorators seem to have one more layer of arrays. This is from the 1.1x manual:

$this->addDecorators(array(
    array('ViewHelper'),
    array('Errors'),
    array('Description', array('tag' => 'p', 'class' => 'description')),
    array('HtmlTag', array('tag' => 'dd')),
    array('Label', array('tag' => 'dt')),
));

So in your case try

'decorators' => array(
    array('ViewHelper'), 
    array('Label', array(
        'tag' => 'dt'
        )
    ),
    array('HtmlTag', array(
        'tag' => 'div',
        'openOnly' => true,
        'id' => 'divDisplayUntil',
        'placement' => 'prepend',
        )
    ),
),
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文