【问题标题】:Autoloading Zend Validators and Filters自动加载 Zend 验证器和过滤器
【发布时间】:2011-12-10 00:05:46
【问题描述】:

我正在尝试自动为 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。我只是无法找到如何为验证器和过滤器自动引导这种自动加载。

谢谢!

【问题讨论】:

    标签: validation zend-framework filter zend-form bootstrapping


    【解决方案1】:

    我将这样的设置添加到我的所有 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
        }
    }
    

    【讨论】:

    • 谢谢,我最终也得到了一个基类实现。列表中的下一个是让它使用我用自动加载器设置的命名空间自动运行 addElementPrefixPath 调用。谢谢
    最近更新 更多