custom form validator

7 messages Options
Embed this post
Permalink
chinaski

custom form validator

Reply Threaded More More options
Print post
Permalink
Hello.

This seems simple, but I can't make it work (path issue). I'm experimenting with Zend_Form, and I want to add a custom validator to a form field.

I have written the a test validator and saved it as follows:

I have saved the file as /My/Validate/TestVal.php.

I have a script that builds the form:

<?php

class forms_ContactForm extends Zend_Form
{
   public function __construct($options = null)
   {
      parent::__construct($options);
     
      $this->addElementPrefixPath('My_Validate','My/Validate/','validate');
     
      $testing = new Zend_Form_Element_Text('testing');
      $testing->setLabel('Title')
         ->setRequired(true);

      $validators = $testing->getValidators();
      $testval   = array('validator' => 'TestVal');
      array_unshift($validators, $testval);
     
      $testing->setValidators($validators);
           
      //etc...
   }
}

This fails at $testing->setValidators with the following message...

Error: Plugin by name My_Validate_TestVal was not found in the registry.

How to get the custom validator attached to the element?

Thanks.

Sam


weierophinney

Re: custom form validator

Reply Threaded More More options
Print post
Permalink
-- chinaski <[hidden email]> wrote
(on Friday, 11 April 2008, 01:38 PM -0700):
> This seems simple, but I can't make it work (path issue). I'm experimenting
> with Zend_Form, and I want to add a custom validator to a form field.
>
> I have written the a test validator and saved it as follows:
>
> I have saved the file as /My/Validate/TestVal.php.

Is that on your include_path? Check that -- it's likely the issue.

The 'My/' directory should be at the same leve as your 'Zend/'
directory:

    application/
        controllers/
        ...
    library/
        Zend/
        My/
            Validate/
                TestVal.php

If it is, then the code you show below should work; if not, then you
have an include_path issue.

>
> I have a script that builds the form:
>
> <?php
>
> class forms_ContactForm extends Zend_Form
> {
>    public function __construct($options = null)
>    {
>       parent::__construct($options);
>      
>       $this->addElementPrefixPath('My_Validate','My/Validate/','validate');
>      
>       $testing = new Zend_Form_Element_Text('testing');
>       $testing->setLabel('Title')
>          ->setRequired(true);
>
>       $validators = $testing->getValidators();
>       $testval   = array('validator' => 'TestVal');
>       array_unshift($validators, $testval);
>      
>       $testing->setValidators($validators);
>            
>       //etc...
>    }
> }
>
> This fails at $testing->setValidators with the following message...
>
> Error: Plugin by name My_Validate_TestVal was not found in the registry.
>
> How to get the custom validator attached to the element?
>
> Thanks.
>
> Sam
>
>
>
> --
> View this message in context: http://www.nabble.com/custom-form-validator-tp16628634p16628634.html
> Sent from the Zend Framework mailing list archive at Nabble.com.
>

--
Matthew Weier O'Phinney
Software Architect       | [hidden email]
Zend - The PHP Company   | http://www.zend.com/
chinaski

Re: custom form validator

Reply Threaded More More options
Print post
Permalink
Thanks for the reply. I fiddled with path and got it to work using:

$element->addPrefixPath('My','My/');

However, the same code does not work when I use add the prefix to the form:

$form->addElementPrefixPath('My','My/');


I thought that adding the prefex path to the form itself made the path available to all of the elements? Am I missing something?

Thanks,
Sam
weierophinney

Re: custom form validator

Reply Threaded More More options
Print post
Permalink
-- chinaski <[hidden email]> wrote
(on Tuesday, 15 April 2008, 02:20 PM -0700):

>
> Thanks for the reply. I fiddled with path and got it to work using:
>
> $element->addPrefixPath('My','My/');
>
> However, the same code does not work when I use add the prefix to the form:
>
> $form->addElementPrefixPath('My','My/');
>
>
> I thought that adding the prefex path to the form itself made the path
> available to all of the elements? Am I missing something?

It does... as long as:

  * you're adding the prefix path *before* creating elements
 
  * AND you're creating the elements via the form object's addElement()
    or createElement() methods

--
Matthew Weier O'Phinney
Software Architect       | [hidden email]
Zend - The PHP Company   | http://www.zend.com/
chinaski

Re: custom form validator

Reply Threaded More More options
Print post
Permalink

Matthew Weier O'Phinney-3 wrote:
> I thought that adding the prefex path to the form itself made the path
> available to all of the elements? Am I missing something?

It does... as long as:

  * you're adding the prefix path *before* creating elements
 
  * AND you're creating the elements via the form object's addElement()
    or createElement() methods
Here is my form code:
class forms_TestvalForm extends Zend_Form
{
   public function __construct($options = null)
   {
      parent::__construct($options);
      $this->addElementPrefixPath('My','My/');
      $this->setName('testval');


      $testing = new Zend_Form_Element_Text('testing');
      $testing->setLabel('Test Value')
      ->setRequired(true);

      //$testing->addPrefixPath('My','My/');

      $testing->addValidator('TestVal');
      $this->addElement($testing);


      $submit = new Zend_Form_Element_Submit('submit');
      $submit->setLabel('submit');

      $this->addElement($submit);

      $this->setAction('/formtest/testval/')
      ->setMethod('post');
   }
}

If I uncomment //testing->addPrefixPath('My,'My/'); it works; otherwise not. Any suggestions as to what I'm doing wrong here?
weierophinney

Re: custom form validator

Reply Threaded More More options
Print post
Permalink
-- chinaski <[hidden email]> wrote
(on Wednesday, 16 April 2008, 10:41 AM -0700):

>
>
>
> Matthew Weier O'Phinney-3 wrote:
> >
> >
> >> I thought that adding the prefex path to the form itself made the path
> >> available to all of the elements? Am I missing something?
> >
> > It does... as long as:
> >
> >   * you're adding the prefix path *before* creating elements
> >  
> >   * AND you're creating the elements via the form object's addElement()
> >     or createElement() methods
> >
> >
> Here is my form code:
> class forms_TestvalForm extends Zend_Form
> {
>    public function __construct($options = null)
>    {
>       parent::__construct($options);
>       $this->addElementPrefixPath('My','My/');
>       $this->setName('testval');
>
>
>       $testing = new Zend_Form_Element_Text('testing');
>       $testing->setLabel('Test Value')
>       ->setRequired(true);

The problem is right here: for the form's elementPrefixPath setting to
have any effect on an element, the element must be created using the
form object's addElement() or createElement() methods. Try this instead:

    $testing = $this->createElement('text', 'testing');

and you should see better results (you'll need to do it for all
methods). If you use createElement(), don't forget to attach the
elements later using addElement(), addElements(), or setElements().


>       //$testing->addPrefixPath('My','My/');
>
>       $testing->addValidator('TestVal');
>       $this->addElement($testing);
>
>
>       $submit = new Zend_Form_Element_Submit('submit');
>       $submit->setLabel('submit');
>
>       $this->addElement($submit);
>
>       $this->setAction('/formtest/testval/')
>       ->setMethod('post');
>    }
> }
>
> If I uncomment //testing->addPrefixPath('My,'My/'); it works; otherwise not.
> Any suggestions as to what I'm doing wrong here?

--
Matthew Weier O'Phinney
Software Architect       | [hidden email]
Zend - The PHP Company   | http://www.zend.com/
chinaski

Re: custom form validator

Reply Threaded More More options
Print post
Permalink
Matthew Weier O'Phinney-3 wrote:
-- chinaski <chinaski@bourbonhours.com> wrote

The problem is right here: for the form's elementPrefixPath setting to
have any effect on an element, the element must be created using the
form object's addElement() or createElement() methods. Try this instead:

    $testing = $this->createElement('text', 'testing');

and you should see better results (you'll need to do it for all
methods). If you use createElement(), don't forget to attach the
elements later using addElement(), addElements(), or setElements().
Thanks Matthew. That works. Here's the final form with the issue solved...

      parent::__construct($options);
      $this->addElementPrefixPath('My','My/');

      $testing = $this->createElement('text','testing');

      $testing->setLabel('Test Value')
      ->setRequired(true);

      $testing->addValidator('TestVal');

      $submit = new Zend_Form_Element_Submit('submit');
      $submit->setLabel('submit');

      $this->addElements(array($testing,$submit));

      $this->setAction('/formtest/testval/')
      ->setMethod('post');