本文介绍了Zend框架-我们应该在哪里放置自定义验证器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以在此处阅读如何写:

We can read here how to write:

class MyValid_Float extends Zend_Validate_Abstract
{

1)
我们应该放在哪里?

应用程序/默认值/验证程序?
application / view / helpers / ...?

application/default/validators ?application/view/helpers/... ?

2)
我们是否必须在应用程序的某个位置注册它?

更新:
这是我的引导程序的示例:

Update:Here's an example of my bootstrap:

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 );

// Prepare the front controller.
$frontController = Zend_Controller_Front::getInstance ();
$frontController->throwExceptions(true);
$frontController->registerPlugin ( new Initializer ( PROJECT_ENV ) );

// Dispatch the request using the front controller.
try {
    $frontController->dispatch ();

} catch ( Exception $exp ) {
    $contentType = "text/html";
    header ( "Content-Type: $contentType; charset=UTF-8" );
    echo "an unexpected error occurred.";
    echo "<h2>Unexpected Exception: " . $exp->getMessage () . "</h2><br /><pre>";
    echo $exp->getTraceAsString ();
}

SO,我是否必须在这里添加:

SO, do I have to add here:

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

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

然后我应该创建一个文件IN :(请注意,此配置使用默认模块):

And then I should create a file IN: (note that this configuration is using default module):

application / default / validators / ValidateSpam.php

并在validateSpam.php上就像这样:

And on validateSpam.php have something like:

class My_Validate_Spam extends Zend_Validate_Abstract {

可以请您确认吗?

谢谢

推荐答案

将您的应用程序/验证器
放到应用程序的Bootstrap类中,添加以下功能:

Place your application/validatorsthen in your application's Bootstrap class, add the following function:

protected function _initAutoload () {

        // configure new autoloader
        $autoloader = new Zend_Application_Module_Autoloader (array ('namespace' => '', 'basePath' => APPLICATION_PATH));

        // autoload validators definition
        $autoloader->addResourceType ('Validator', 'validators', 'Validator_');
}

有关

另一种方式在博客,其中使用此自定义验证器的表单的控制器构造函数有一条额外的行:

Another way is described in this blog, where the constructor of the controller for the form that is using this custom validator has an extra line:

class JD_Form_Controller extends Zend_Form
{
 public function __construct($options = null)
 {
   // path setting for custom classes MUST ALWAYS be first!
   $this->addElementPrefixPath('JD_Form_Validator','JD/Form/Validator','validate');
   ...
 }
 ...
}

这篇关于Zend框架-我们应该在哪里放置自定义验证器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 08:05