Zend Form Custom Validation Path issues

人走茶凉 提交于 2020-01-16 13:22:08

问题


The issue:

Plugin by name 'Spam' was not found in the registry; used paths: Zend_Validate_: Zend/Validate/

I have this on my bootstrap.php file (it's NOT a class):

include_once 'config_root.php';
set_include_path ( $PATH );

require_once 'Initializer.php';
require_once "Zend/Loader.php";
require_once 'Zend/Loader/Autoloader.php';

// Set up autoload.
$loader = Zend_Loader_Autoloader::getInstance ();
$loader->setFallbackAutoloader ( true );
$loader->suppressNotFoundWarnings ( false );

//resource Loader
$resourceLoader = new Zend_Loader_Autoloader_Resource(array(
                'basePath' => APPLICATION_PATH,
                'namespace' => '',
            ));

$resourceLoader->addResourceType('validate', 'validators/', 'My_Validate_');

$loader->pushAutoloader($resourceLoader);

I've named a file called Spam.php like this:

application/validators/Spam.php

class My_Validate_Spam extends Zend_Validate_Abstract {

On the form class I have:

//HONEY POT
        $this->addElement(
                'text', 'honeypot', array(
                    'label' => 'Honeypot',
                    'required' => false,
                    'class' => 'honeypot',
                    'decorators' => array('ViewHelper'),
                    'validators' => array(
                        array(
                            'validate' => 'Spam'
                        )
                    )
                )
        );

With all this, I'm getting:

Plugin by name 'Spam' was not found in the registry; used paths: Zend_Validate_: Zend/Validate/

Why ?

Thanks a lot.


回答1:


You have to add the directory where you have your custom validators to your form elements prefix path. For example:

$elementPrefixPaths = 
    array(
        array(
            array(
                'prefix' => 'My_Validate_', 
                'path' => 'My/Validate', // 'application/validators' in your case
                'type' => 'validate',
            )
        )
    );
$form->addElementPrefixPaths($elementPrefixPaths);
// or, if your're inside the form, 
// $this->addElementPrefixPaths($elementPrefixPaths)
// before any elements make use of the validator.

The 'path' should be in your include path. You have to do the same with your custom filters. Also there is a similar approach for custom decorators and elements (which use the method setPrefixPaths() instead).

Read more here.

Your path is 'application/validators', but it would be better to follow ZF convention on class naming and path mirroring; as such you should put your validator in a directory such as 'My/Validate' You should follow this convention on all custom ZF extensions you develop (filters, helpers, plugins, etc). It will make your life easier in the long run. Also, as a final suggestion, don't use "My_" as your classes prefix, use something more personal, such as "Mem_" (considering your nickname).



来源:https://stackoverflow.com/questions/6444879/zend-form-custom-validation-path-issues

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!