ZF-5870: Zend_Form::isValid() should not expect an array on a prepopulated form
Description
Developers should be allowed to do something like this:
// fill the form and perform some random operations $form = new Some_Form(); $form->populate($data);
// validation if ($form->isValid()) { // proceed }
Right now there's an exception when isValid is used without its expected parameter: "Zend_Form::isValid expects an array" Although, isValid($data) also populates, in some cases a separate populate() and isValid() calls is better code.
Comments
Posted by Michael Ekoka (verysimple) on 2009-02-23T19:35:25.000+0000
Unecessary details removed
Posted by Matthew Weier O'Phinney (matthew) on 2009-02-23T19:49:54.000+0000
Unfortunately, this change cannot be made as suggested,as it would break an important aspect of Zend_Form. Zend_Form implements Zend_Validate_Interface, and as such, has one required argument to isValid().
What we may be able to do is to allow passing a special argument to isValid() indicating it should validate with the populated values.
Posted by Ryan Mauger (bittarman) on 2009-09-22T03:10:59.000+0000
I actually don't feel this is a good thing. populate() proxies setDefaults(), which does exactly what it says on the tin. Keeping the values array required in isValid() keeps this a clear distinction between the intentions of the methods. to make this optional would only serve to blur this line.
Posted by Erwin Derksen (erwind) on 2009-11-23T02:07:07.000+0000
I feel this would be a good thing. Requiring an array as input to isValid() seems to me like you are not validating the form but a set of values external to the form.
Thus: use the setDefaults/setDefault to populate the form some way or another and then call isValid on the form.
As Matthew Weier O'Phinney indicates, changing this would break conformance with the validate interface. So the change should be in the validator interface. The current interface treats a validator as an object only containing the rules, not the value. However, in many cases the validator is the object also containing the value. An isValid() method without arguments thus seems logical. This holds for form elements and for forms.
Hmmm, thinking about this, I see a Validatable and Validator interface with methods isValid() resp. validate($value) (both having an optional $context parameter). This would break B.C., but in an easy to fix way.
Shortcut solution for the form would be to allow null (not an empty array) as the special argument. However, I think that implementing this at the form level, would imply implementing a similar solution at the form elements level! (and I'm not sure that null will do as fine here as at the form level)
Posted by Matthew Weier O'Phinney (matthew) on 2009-11-23T06:02:08.000+0000
This may be a moot point once 2.0 is out the door anyways, as the validation would happen on the validation chain attached to the form. That said, I'll take this into consideration for 2.0.
I'm marking this as postponed for now.
Posted by Claude Duvergier (cduv) on 2009-12-10T08:51:09.000+0000
@Ryan Mauger: Actually {{Zend_Form::isValid()}} also populates the form (because it calls {{setValue()}} on each {{Zend_Form_Element}})
To me, the current behavior of {{Zend_Form_Element::isValid($someValue)}} looks like a {{Zend_Form_Element::setValue($someValue)}} + {{Zend_Form_Element::isValid()}} combo-shortcut.