【问题标题】:Zend Framework - Where should we place our custom Validators?Zend Framework - 我们应该将自定义验证器放在哪里?
【发布时间】:2011-06-22 13:02:34
【问题描述】:

我们可以在这里阅读如何编写:

http://framework.zend.com/manual/en/zend.validate.writing_validators.html

class MyValid_Float extends Zend_Validate_Abstract
{

1) 我们应该把它放在哪里?

应用程序/默认/验证器? 应用程序/视图/助手/...?

2) 我们必须在应用程序的某个地方注册吗?

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

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

所以,我必须在这里添加:

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

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

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

application/default/validators/ValidateSpam.php

在 validateSpam.php 上有类似的东西:

class My_Validate_Spam extends Zend_Validate_Abstract {

你能确认一下吗?

谢谢

【问题讨论】:

  • 如果没有创建这个类的特定位置,请告诉我你通常放置它的位置。如果您需要或不需要以某种方式在应用程序中注册。

标签: zend-framework zend-validate custom-validators


【解决方案1】:

将您的application/validators 然后在应用程序的 Bootstrap 类中,添加以下函数:

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_');
}

更多关于Zend Bootstrap Autoloading的细节

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

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');
   ...
 }
 ...
}

【讨论】:

  • @Ozair Kafray:我们可以为它创建任何路径吗?我的意思是,我们可以在 /Form/ 中创建一个名为 Validator 的文件夹吗?没有 Zend 知道吗?
  • @Ozar Kafray:我正在尝试按照您的链接说明(关于已接受的答案)它说:“为了在此文件夹下包含文件,我们需要创建一个 Zend 资源自动加载器的实例“ - 我的问题是:我们应该把代码放在哪里?其他人似乎认为该文件应该在某个地方,每个人都知道 - 也许我在这里遗漏了一些信息?
  • 好的,这是一个让您了解自动加载概念的链接。我在我的 Bootstrap 类的 _initAutoload () 函数中定义了自动加载。在此函数中,每种类型的资源都有一行,例如$autoloader-&gt;addResourceType ('Form', 'forms', 'Form_');。稍后我会尝试在我的答案中粘贴一些代码。
  • @Ozair Kafray:我已经更新了我的问题,我只需要你的确认。非常感谢。
  • @Ozair Kafray:我的引导文件中没有 class 关键字,因此,我相信该功能不起作用。 :( 但也许我在示例中所做的方式可行,我只是注意到您有 Module_Autoloader 而不是 Module_Autoloader_Resource ...也许我可以改变它。
【解决方案2】:

我通过将以下行添加到 application.ini 来做到这一点:-

autoloadernamespaces[] = "App_"

然后我将自定义验证器放入(例如)/library/App/Validate/MyCustomValidator.php。

然后我可以使用以下代码编写我的验证器:-

class App_Validate_MyCustomValidator() extends Zend_Validate_Abstract

它对我来说效果很好,而且简单易实现。

【讨论】:

  • 你的命名空间“App_”是否与 /library/App/ 中的文件夹 App 相关联?
  • 是的,Zend 会自行处理,要使用验证器,我会执行类似 myValidator = new App_Validate_MyCustomValidator() 的操作。上面添加到 application.ini 的行告诉自动加载器在哪里可以找到我的 App_ 命名空间。
猜你喜欢
  • 2012-04-16
  • 1970-01-01
  • 1970-01-01
  • 2011-07-12
  • 1970-01-01
  • 2018-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多