3

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

MEM
  • 30,529
  • 42
  • 121
  • 191
  • If there's no specific place for creating this class, please tell me where you normally place it. And if you need or not to registered somehow within the application. – MEM Jun 22 '11 at 13:07

2 Answers2

6

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');
   ...
 }
 ...
}
Ozair Kafray
  • 13,351
  • 8
  • 59
  • 84
  • @Ozair Kafray: And can we just create any path for it ? I mean, can we create a folder inside /Form/ called Validator ? Without Zend to know about ? – MEM Jun 22 '11 at 13:20
  • @Ozar Kafray: I'm trying to follow your link instructions (about the accepted answer)and it says: "In order to include files under this folder we need to create an instance of zend resource autoloader" - and my question is: Where should we place that code ? It seems taken for everyone else that that file should be somewhere, that everybody knows about - perhaps I'm missing some information here ? – MEM Jun 22 '11 at 13:30
  • 1
    Ok, that was a link to give you the concept of Autoloading. I defined auto loading in `_initAutoload ()` function of my Bootstrap class. In this function there is a line for each type of resource such as `$autoloader->addResourceType ('Form', 'forms', 'Form_');`. I will try to paste some code in my answer in a while. – Ozair Kafray Jun 22 '11 at 13:40
  • @Ozair Kafray: I have updated my question, I would just need a confirmation from you. Thanks a lot. – MEM Jun 22 '11 at 13:57
  • @Ozair Kafray: I have no class keyword on my bootstrap file, hence, I believe that function will not work. :( But perhaps the way I did on my example could work, I just notice that you have Module_Autoloader and NOT Module_Autoloader_Resource ... perhaps I could change that. – MEM Jun 22 '11 at 14:36
  • Yes, my bootstrap class is defined like this `class Bootstrap extends Zend_Application_Bootstrap_Bootstrap` – Ozair Kafray Jun 22 '11 at 14:44
  • @ozair Kafray - This will be a long comment thing, so I end accepting your answer. I still have an issue, and I've passed to a new question here: http://stackoverflow.com/questions/6442852/zend-honey-pot-validation - About that class, all the application is working without that class declaration and I'm not sure if I just can add. I would prefer to implement this without to many changes on the application configuration (that I haven't made. :s) – MEM Jun 22 '11 at 16:09
1

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.

vascowhite
  • 18,120
  • 9
  • 61
  • 77
  • and is you namespace "App_" associated with your folder App in /library/App/ somehow ? – MEM Jun 22 '11 at 15:15
  • Yes, Zend looks after that itself ,to use the validator I would do something like myValidator = new App_Validate_MyCustomValidator(). The line added to application.ini above tells autoloader where to find my App_ namespace. – vascowhite Jun 22 '11 at 23:10