在全局应用程序级别更改默认的 Zend Form Decorator?

发布于 2025-01-03 13:15:49 字数 189 浏览 1 评论 0原文

当然,我不想改变库/Zend。 我知道我可以创建类 My_Form 来扩展 Zend_Form 并设置自定义装饰器。然后,每个表单都会扩展新类 My_Form...

有没有办法在某些插件或引导程序中设置 Zend Form Decorator(更改默认装饰),而不更改现有表单?

或者,覆盖所有表单的默认表单装饰器的最佳方法是什么?

Of course, I don't want to change library/Zend.
I know that I can create class My_Form which extends Zend_Form and setup custom decorator. Than, every form extends new class My_Form...

Is there any way to set Zend Form Decorator(change default decoration) in some plugin or bootstrap, without changing existing forms ??

Or, What is the best way to override default form decorator for all forms?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

北方的韩爷 2025-01-10 13:15:49

我不确定我的回答是否有帮助,但你可以看看。
有一种方法可以用您自己的装饰器替换 ZF 装饰器,而无需编辑表单本身。

解决方案#1:

描述的方法这里。或者简而言之:

假设您有一个表单:

class Form extends Zend_Form
{
    function init ()
    {
        $this->addElement ('text', 'a', array (
            'label' => 'Name'
        ));
    }
}

然后在 application.ini 中

resources.view.helperPath.Default_View_Helper = APPLICATION_PATH "/views/helpers"

添加一个新文件 application/views/helpers/FormText.php

class Default_View_Helper_FormText extends Zend_Form_Decorator_Abstract
{
    function formText ()
    {
        return 'It is I.';
    }
}

就是这样。

解决方案#2:

让我们有这个抽象类:

abstract class Application_Style
{
    private $_object;



    function __construct ($object = null)
    {
        if (isset ($object))
        {
            $this->apply ($object);
        }
    }


    function apply ($object)
    {
        $this->setObject ($object);
        if ($this->filter ())
        {
            $this->onApply ();
        }

        return $object;
    }


    function __call ($method, $arguments)
    {
        return call_user_func_array (array ($this->getObject (), $method), $arguments);
    }


    abstract protected function onApply ();


    protected function filter ()
    {
        return true;
    }


    function setObject ($_object)
    {
        $this->_object = $_object;
    }


    function getObject ()
    {
        return $this->_object;
    }
}

然后是一个后代。

class Application_Style_AdminForm extends Application_Style
{
    function onApply ()
    {
            $this->addElement ($submit = new Zend_Form_Element_Submit ('submit', array(
            'label' => 'Submit',
            )));

            $submit
            ->removeDecorator ('DtDdWrapper')
            ->addDecorator ('HtmlTag', array (
            'placement' => Zend_Form_Decorator_HtmlTag::PREPEND,
            'tag' => 'p',
            'openOnly'  => 1,
            ))
            ->addDecorator ('Custom', array ('text' => '     '))
            ;
        }
}

onApply() 方法中可以是任何适合您的内容。例如,添加或删除装饰器。然后,您可以像这样在表单上调用此样式:

new Application_Style_AdminForm ($this);

这允许您操作表单表示,但不直接更改它。

I am not sure whether my answer will be of help, but here you go.
There is a way of substituting ZF decorators with your own decorators without editing the forms themselves.

SOLUTION #1:

The method described here. Or in short:

Let's say you've got a form:

class Form extends Zend_Form
{
    function init ()
    {
        $this->addElement ('text', 'a', array (
            'label' => 'Name'
        ));
    }
}

Then have in application.ini

resources.view.helperPath.Default_View_Helper = APPLICATION_PATH "/views/helpers"

Add a new file application/views/helpers/FormText.php

class Default_View_Helper_FormText extends Zend_Form_Decorator_Abstract
{
    function formText ()
    {
        return 'It is I.';
    }
}

And that is it.

SOLUTION #2:

Let's have this abstract class:

abstract class Application_Style
{
    private $_object;



    function __construct ($object = null)
    {
        if (isset ($object))
        {
            $this->apply ($object);
        }
    }


    function apply ($object)
    {
        $this->setObject ($object);
        if ($this->filter ())
        {
            $this->onApply ();
        }

        return $object;
    }


    function __call ($method, $arguments)
    {
        return call_user_func_array (array ($this->getObject (), $method), $arguments);
    }


    abstract protected function onApply ();


    protected function filter ()
    {
        return true;
    }


    function setObject ($_object)
    {
        $this->_object = $_object;
    }


    function getObject ()
    {
        return $this->_object;
    }
}

And then a descendant.

class Application_Style_AdminForm extends Application_Style
{
    function onApply ()
    {
            $this->addElement ($submit = new Zend_Form_Element_Submit ('submit', array(
            'label' => 'Submit',
            )));

            $submit
            ->removeDecorator ('DtDdWrapper')
            ->addDecorator ('HtmlTag', array (
            'placement' => Zend_Form_Decorator_HtmlTag::PREPEND,
            'tag' => 'p',
            'openOnly'  => 1,
            ))
            ->addDecorator ('Custom', array ('text' => '     '))
            ;
        }
}

In the onApply() method could be anything that suits you. Adding or removing decorators, for example. Then you can call this styling upon your form like this:

new Application_Style_AdminForm ($this);

which allows you to manipulate the form representation but without changing it directly.

老子叫无熙 2025-01-10 13:15:49

许多人都尝试过,据我所知,没有人在不扩展 Zend_Form 的情况下成功做到这一点。请参阅用于子-最优解。

Many have tried, none that I know of succeeded in doing it without extending Zend_Form. See this and this for sub-optimal solutions.

流年已逝 2025-01-10 13:15:49

我有一个解决方案。您可以在 bootsrap 中定义装饰器。

例如:-

$textDecorator = array(
                array('ViewHelper',
                    array('helper' => 'formText')
                ),
                array('Label',
                    array('class' => 'label')
                ),
                array('HtmlTag',
                    array('tag' => 'div', 'class' => 'formfield clearfix')
                )
            ); 

Zend_Registry::set('text_dec', $textDecoration);

现在您可以将它用于所有表单文本字段。

例如:-

class TestForm extends Zend_Form
{
    function init ()
    {
        $this->addElement ('text', 'a', array (
            'label' => 'Name',
            'decorator' => Zend_Registry::get('text_dec')
        ));
    }
}

所以你可以使用这个的一些全局装饰器。

I have one solution. You can define decorators in bootsrap.

ex:-

$textDecorator = array(
                array('ViewHelper',
                    array('helper' => 'formText')
                ),
                array('Label',
                    array('class' => 'label')
                ),
                array('HtmlTag',
                    array('tag' => 'div', 'class' => 'formfield clearfix')
                )
            ); 

Zend_Registry::set('text_dec', $textDecoration);

Now you can use it for all form text field.

ex:-

class TestForm extends Zend_Form
{
    function init ()
    {
        $this->addElement ('text', 'a', array (
            'label' => 'Name',
            'decorator' => Zend_Registry::get('text_dec')
        ));
    }
}

So you can use some global decorator form this one.

∞觅青森が 2025-01-10 13:15:49

这是完整的解决方案。

在“.ini”文件中为装饰器设置文件夹:

; folder with custom decorators will be loaded for FormElements
form.elementPrefixPath.decorator.prefix =  "Application_Form_Decorator"
form.elementPrefixPath.decorator.path   =  "Application/Form/Decorator/"
form.elementPrefixPath.decorator.type   = "decorator"

; folder with custom decorators will be loaded for Forms
form.prefixPath.decorator.prefix =  "Application_Form_Decorator"
form.prefixPath.decorator.path   =  "Application/Form/Decorator/"
form.prefixPath.decorator.type   = "decorator"

接下来,为装饰器使用相同名称以覆盖默认装饰器。
例如,要替换默认的“DtDdWrapper”装饰器,
您可以使用 Application_Form_Decorator_DtDdWrapper:

class Application_Form_Decorator_DtDdWrapper extends Zend_Form_Decorator_DtDdWrapper

装饰器将由该名称的最后一部分加载。

Here is complete solution.

Set folders for your decorators in ".ini" file:

; folder with custom decorators will be loaded for FormElements
form.elementPrefixPath.decorator.prefix =  "Application_Form_Decorator"
form.elementPrefixPath.decorator.path   =  "Application/Form/Decorator/"
form.elementPrefixPath.decorator.type   = "decorator"

; folder with custom decorators will be loaded for Forms
form.prefixPath.decorator.prefix =  "Application_Form_Decorator"
form.prefixPath.decorator.path   =  "Application/Form/Decorator/"
form.prefixPath.decorator.type   = "decorator"

Next, use same name for your decorator to override default decorator.
For example, to substitute default "DtDdWrapper" decorator,
you can use Application_Form_Decorator_DtDdWrapper:

class Application_Form_Decorator_DtDdWrapper extends Zend_Form_Decorator_DtDdWrapper

Decorator will be loaded by last part of that name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文