0

I have this in a Zend_Form's init method:

$username_validators = array(
    'Alpha',
    array('StringLength', false, array(3, 20)),
);

$some_form->addElement('text', 'username', array(
    'filters'    => array('StringTrim', 'StringToLower'),
    'validators' => $username_validators,
    'required'   => true,
    'label'      => 'Username:',
));

Is it possible to create a Zend_Validate object that loads the same validators array that I'm passing addElement? It would be something like:

$v = new Zend_Validate();
//this is the part I'm unsure. Zend_Validate doesn't have an addValidators method.
$v->addValidators($username_validators);
echo $v->isValid('testuser1');
cambraca
  • 27,014
  • 16
  • 68
  • 99

2 Answers2

1

Sure you can add a collection of validators from a member variable, as long as they don't require any dynamic options that need to be specified at instantiation.

Edit

It appears to me that, out of the box, you cannot do something similar. Zend_Form has a plugin loader/registry that enables you to use "short forms" for validators. The plugin loader is configured with paths and class prefixes that allow it to actually create true validator instances from the short forms and any provided validator options.

In contrast, the code of Zend_Validate::addValidator() appears to actually require an actual validator instance.

But it looks like you could kind of piggyback on this form/element registry as follows: create a form element, assign short form validators to the element, call getValidators() on the element (Zend_Form_Element::getValidators() seems to convert each short form validator into a real instance), and then feed these validators one at a time into Zend_Validate. Seems to be a long way around, but it should work.

David Weinraub
  • 14,144
  • 4
  • 42
  • 64
  • I'm not sure how this answers my question.. how do I tell my `$v` object which are the validations from an arbitrary `$username_validators` array? – cambraca Mar 21 '11 at 16:30
  • @cambraca: You are totally right. My answer was completely brain-dead; obviously need more coffee. Edited the answer to give something that is hopefully closer to *this* planet. ;-) – David Weinraub Mar 21 '11 at 18:52
  • thank you very much! It seems like an idea that would work, though I'm a bit concerned about memory use (and elegance of the code).. I guess I'll go back to looping the array myself and doing `Zend_Validate::is(..)` on each one... – cambraca Mar 21 '11 at 22:59
  • Well, for elegance - at least for the API - you could hide it all inside a custom My_Validate class that extends `Zend_Validate`. Internally, the custom class could have an $element member all configured with paths and prefixes. Then override `addValidator()` to deal with the short forms. Only the class itself would know about the contrivance; from the outside looking in, it would be similar to `Z_V` itself. Memory use would certainly be higher, but no more than with a form. Anyway, thanks for the chance to redeem myself. ;-) – David Weinraub Mar 22 '11 at 03:00
0

Yes, you can do what you want as long as $username_validators has been declared and is accessible in the scope of the function / class. If you are using a class, you will declare a private variable:

private $userVariables;

Then in the constructor populate it:

public function __construct()
{
    $this->userVariables = array(
        //validator options here
    );
}

You can now assign this single validator as many times as you like by calling $this->userVariables:

$v = new Zend_Validate();
$v->addValidators($this->userVariables); //this is the part I'm unsure
echo $v->isValid('testuser1');
Richard Parnaby-King
  • 14,703
  • 11
  • 69
  • 129
  • I guess my question is unclear.. the "addValidators" function doesn't exist. I just want to know how I can get the validators, from the array format, into the Zend_Validate object. I just edited my question.. – cambraca Mar 21 '11 at 18:44