-- leedavis <[email protected]> wrote
(on Friday, 08 April 2011, 07:15 AM -0700):
> The thing I cant get my head around, is, if forms can be in an error state
> using methods such as setError(s), addErrorMessage(s) etc. Then why cant we
> attach validators to a form object?
> 
> Why are these limited to form elements only? It could be that I'm completely
> missing something or simply not using Zend_Form as intended. But we have an
> error messages stack and status encapsulated within the form object, why
> cant we attach validators directly that will manipulate this, exactly how
> you would to a form element?

There's several reasons. 

First, in order to have re-usable elements, and in order to allow chains
of validators per-element, validators must be attached per-element, and
must be stateful per-element. For example, if I have a Between
validator, I may have different min/max ranges per-element.

Second, because of the above decision, at first, Zend_Form was simply
validating elements and returning the aggregate; if any individual
element failed validation, the form did as well, plain and simple. The
ability to mark a form as invalid and attach errors and messages was
added later as developers indicated these features would be useful.

Nobody has yet requested a form-level validator -- except in the context
of having a validator chain that could be shared between a domain entity
and the form object. However, this is fairly contrary to the original
design decision outlined above, and we feel it would require a fair
amount of architectural change to accomplish. As such, we're taking that
idea in mind for refactoring for ZF2 -- but not before.

> wouldn't it be nicer if I could just add validators directly to my form,
> such as..
> 
>   
> class App_Form_Payment extends Zend_Form
> {
>   public function __construct ()
>   {
>      $this->addValidator(new App_Form_Validators_SinglePayment());
>   }
> }
> 
> Thoughts?

-- 
Matthew Weier O'Phinney
Project Lead            | [email protected]
Zend Framework          | http://framework.zend.com/
PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc

-- 
List: [email protected]
Info: http://framework.zend.com/archives
Unsubscribe: [email protected]


Reply via email to