简体   繁体   中英

Using Custom Zend Validator in INI file

I don't have a custom Zend_Form and I just declared all the elements in the ini file. There is no problem with creating the Zend_Form from the ini file, but I am having problem using my own custom validator in my ini file. It always return Not Found In Registry error.

Currently, my code is like this.

[Bootstrap]

$resourceLoader = new Zend_Loader_Autoloader_Resource(array(
            'namespace' => 'MY',
            'basePath'  => dirname(__FILE__)
        ));

$resourceLoader->addResourceType('validator', 'forms/validate/', 'Form_Validate');

[ini file]
form.elements.new_password.options.validators.password.validator = "Password"

[Custom Validator]
<?php 

class MY_Form_Validate_Password extends Zend_Validate_Abstract
{
......

Please tell me what I'm missing here. Thanks!

This is a case of over thinking:

I'm going to assume you're using a version of ZF1.x that's fairly new (v1.10 and newer).

You have a file called project/application/configs/application.ini

in a standard application.ini is the line:

includePaths.library = APPLICATION_PATH "/../library"

this tells us that all of our library code (a custom validator qualifies) would belong to the directory at this path.

Also there is the line:

autoloaderNamespaces[] = "MY_"

This tells us the name of our library (defines the directory below /library).

so our custom validator MY_Form_Validate_Password would live at:

project
    /library
        /MY 
            /Form
                /Validate
                    Password.php

nothing else is required, all that bootstrap code is only needed if you are going to do something not anticipated by Zend Framework, also validators are not typically registered as resources.

The class can be called as

$validator = new MY_Form_Validate_Password();

as usual and any other access method should work as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM