自动加载 Zend 验证器和过滤器

发布于 2024-12-20 12:12:26 字数 864 浏览 3 评论 0原文

我正在尝试自动设置 Zend Form 验证器和过滤器的前缀路径。我做了很多研究并找到了很多解决方案,但我希望这些解决方案始终自动生效。我知道我可以以以下形式执行此操作:

$this->addElementPrefixPath('My_Validate', 'My/Validate/', 'validate');

或将它们设置在一个元素上,例如

$input->addValidatorPrefixPath('Other_Namespace', 'Other/Namespace');
$input->addFilterPrefixPath('Foo_Namespace', 'Foo/Namespace');

,但是有没有办法让这些元素自动查看自动加载器中已经设置的内容和/或在引导程序(或其他地方)中设置,而无需再次设置它吗?

这是我的自动加载器:

// Autoload libraries
$autoloader = Zend_Loader_Autoloader::getInstance();    
$autoloader->registerNamespace('Lib1_')
   ->registerNamespace('Lib2_')
   ->registerNamespace('Lib3_');

现在,当我使用添加验证器时,

->addValidator('CustomValidator', false, 1)

我希望它尊重自动加载器中列出的层次结构,然后返回到 Zend。我只是无法找到如何自动引导验证器和过滤器的这种自动加载。

谢谢!

I'm trying to set the prefix path for Zend Form's validators and filters automatically. I've done a lot of research and found many solutions, but I want these to always take effect, automatically. I know I can do this in the form:

$this->addElementPrefixPath('My_Validate', 'My/Validate/', 'validate');

or set them on an element such as

$input->addValidatorPrefixPath('Other_Namespace', 'Other/Namespace');
$input->addFilterPrefixPath('Foo_Namespace', 'Foo/Namespace');

but is there any way for these to automatically look into what's already set in the autoloader and/or be set in the bootstrap (or elsewhere), without having to set it ever again?

Here's my autoloader:

// Autoload libraries
$autoloader = Zend_Loader_Autoloader::getInstance();    
$autoloader->registerNamespace('Lib1_')
   ->registerNamespace('Lib2_')
   ->registerNamespace('Lib3_');

Now, when I add a validator using

->addValidator('CustomValidator', false, 1)

I would like it to respect the hierarchy listed in the autoloader, falling back to Zend after that. I just haven't been able to find how to automatically bootstrap this kind of autoloading for the validators and filters.

Thanks!

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

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

发布评论

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

评论(1

注定孤独终老 2024-12-27 12:12:26

我将这样的设置添加到我的所有 Zend 表单中而不必为每个表单重复代码的方法是创建一个扩展 Zend_Form 的基本表单类,而我的所有其他表单又扩展它。

在基本表单的构造函数中,我为各种类型的元素设置不同的装饰器,或者为我的应用程序自定义装饰器,为帮助器和验证器等指定前缀路径。

关于此方法需要注意的重要一点是,如果您的基本形式 __construct 方法,则必须调用 parent::__construct() 作为最后一行。这样做的原因是因为 Zend_Form::init() 方法被 Zend_Form::__construct() 调用,之后构造函数中没有任何其他内容运行。

这是一个例子:

<?php

class Application_Form_Base extends Zend_Form
{
    // decorator spec for form elements like text, select etc.
    public $elementDecorators = array(
        'ViewHelper',
        'Errors',
        array('Description', array('tag' => 'p', 'class' => 'description', 'escape' => false)),
        array('HtmlTag', array('class' => 'form-div')),
        array('Label', array('class' => 'form-label', 'requiredSuffix' => '*'))
    );

    // decorator spec for checkboxes
    public $checkboxDecorators = array(
        'ViewHelper',
        'Errors',
        array('Label', array('class' => 'form-label', 'style' => 'display: inline', 'requiredSuffix' => '*', 'placement' => 'APPEND')),
        array('HtmlTag', array('class' => 'form-div')),
        array('Description', array('tag' => 'p', 'class' => 'description', 'escape' => false, 'placement' => 'APPEND')),
    );

    // decorator spec for submits and buttons
    public $buttonDecorators = array(
        'ViewHelper',
        array('HtmlTag', array('tag' => 'div', 'class' => 'form-button'))
    );

    public function __construct()
    {
        // set the <form> decorators
        $this->setDecorators(array(
                             'FormElements',
                             array('HtmlTag', array('tag' => 'div', 'class' => 'form')),
                             'Form'));

        // set this as the default decorator for all elements added to the form
        $this->setElementDecorators($this->elementDecorators, array('submit', 'button'), true);

        // add prefix paths for decorators and validators
        $this->addElementPrefixPath('My_Decorator', 'My/Decorator', 'decorator');
        $this->addElementPrefixPath('My_Validator', 'My/Validator', 'validate');

        parent::__construct();
        // parent::__construct must be called last because it calls $form->init()
        // and anything after it is not executed
    }
}

The way I get settings like this into all of my Zend forms without having to repeat the code all over for each form is to create a base form class that extends Zend_Form, which in turn all of my other forms extend.

In the base form's constructor, I set up different decorators for various types of elements or customize the decorators for my application, specify prefix paths for helpers and validators and other things.

The important thing to note about this method, is you must call parent::__construct() as the very last line if your base form __construct method. The reason for this is because the Zend_Form::init() method gets called by Zend_Form::__construct() and nothing else from the constructor runs after that.

Here is an example:

<?php

class Application_Form_Base extends Zend_Form
{
    // decorator spec for form elements like text, select etc.
    public $elementDecorators = array(
        'ViewHelper',
        'Errors',
        array('Description', array('tag' => 'p', 'class' => 'description', 'escape' => false)),
        array('HtmlTag', array('class' => 'form-div')),
        array('Label', array('class' => 'form-label', 'requiredSuffix' => '*'))
    );

    // decorator spec for checkboxes
    public $checkboxDecorators = array(
        'ViewHelper',
        'Errors',
        array('Label', array('class' => 'form-label', 'style' => 'display: inline', 'requiredSuffix' => '*', 'placement' => 'APPEND')),
        array('HtmlTag', array('class' => 'form-div')),
        array('Description', array('tag' => 'p', 'class' => 'description', 'escape' => false, 'placement' => 'APPEND')),
    );

    // decorator spec for submits and buttons
    public $buttonDecorators = array(
        'ViewHelper',
        array('HtmlTag', array('tag' => 'div', 'class' => 'form-button'))
    );

    public function __construct()
    {
        // set the <form> decorators
        $this->setDecorators(array(
                             'FormElements',
                             array('HtmlTag', array('tag' => 'div', 'class' => 'form')),
                             'Form'));

        // set this as the default decorator for all elements added to the form
        $this->setElementDecorators($this->elementDecorators, array('submit', 'button'), true);

        // add prefix paths for decorators and validators
        $this->addElementPrefixPath('My_Decorator', 'My/Decorator', 'decorator');
        $this->addElementPrefixPath('My_Validator', 'My/Validator', 'validate');

        parent::__construct();
        // parent::__construct must be called last because it calls $form->init()
        // and anything after it is not executed
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文