2

I've split up a Form into 3 SubForms and for one of the elements, in the last SubForm, I am creating a Validator that extends Zend_Validator_Abstract.

This validator needs to check that a value, on the second SubForm, is not empty. However this value will not be in the $context array for the element in the Third SubForm.

What is a sensible way of making this value available in the $context across SubForms?

...

After some thought, the only way I can think of doing this is to pass a reference of the parent Form to the Validator's constructor despite it breaking encapsulation.

Charles
  • 50,943
  • 13
  • 104
  • 142
gawpertron
  • 1,867
  • 3
  • 21
  • 37

1 Answers1

3

You might also try overriding the form object isValid() method and utilizing the $data variable that is available there.

For example:

<?php 
class MyForm extends Zend_Form {
    public function isValid($data) {
        // check $data['fieldname'] or add a new validator here, then...
        return parent::isValid($data);
    }
}
Tomáš Fejfar
  • 11,129
  • 8
  • 54
  • 82
Wil Moore III
  • 6,968
  • 3
  • 36
  • 49
  • How would you use Zend_Filter_Input in conjuction with Zend_form? – gawpertron Aug 19 '10 at 18:47
  • Zend_Filter_Input looks like a striped down Zend_Form that purely handles Filters and Validators. It looks useful if you have a complex form with many decorators and complex validation rules. You could separate responsibility or provide alternative filter and validation, without breaking the base form. Cheers for the tip – gawpertron Sep 07 '10 at 10:31
  • 1
    Just a note - it's not obvious whether you are talking about $myform->isValid() or $myvalidator->isValid(). It took me a while to work out you were referring to the former – asgeo1 Nov 12 '10 at 02:21
  • @asgeo: Thanks...I up-voted your comment as I over-looked that. Editing now. – Wil Moore III Nov 15 '10 at 22:25