问题
We can read here how to write:
http://framework.zend.com/manual/en/zend.validate.writing_validators.html
class MyValid_Float extends Zend_Validate_Abstract
{
1) Where should we place this?
application/default/validators ? application/view/helpers/... ?
2) Do we have to register this somewhere on our application ?
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, do I have to add here:
$resourceLoader = new Zend_Loader_Autoloader_Resource(array(
'basePath' => APPLICATION_PATH,
'namespace' => '',
));
$resourceLoader->addResourceType('validate', 'validators/', 'My_Validate_');
And then I should create a file IN: (note that this configuration is using default module):
application/default/validators/ValidateSpam.php
And on validateSpam.php have something like:
class My_Validate_Spam extends Zend_Validate_Abstract {
Can you please confirm ?
Thanks
回答1:
Place your application/validators
then 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_');
}
More detail(s) about Zend Bootstrap Autoloading.
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');
...
}
...
}
回答2:
I do it by adding the following line to application.ini :-
autoloadernamespaces[] = "App_"
Then I put my custom validators in (for example) /library/App/Validate/MyCustomValidator.php.
I can then write my validator using something like:-
class App_Validate_MyCustomValidator() extends Zend_Validate_Abstract
It works pretty well for me and is simple and easy to implement.
来源:https://stackoverflow.com/questions/6440308/zend-framework-where-should-we-place-our-custom-validators