Validating your data from a zend form object.

In most cases validation would typically be attributed directly to the form
element in question, for example validating an "email address" may use the
EmailAddress validator shipped with ZF or you may want to create your own to
ensure the email address isn't already registered, requiring a little more
than a simple syntax check.

However, I need to append a validator to a form itself. I have a small form
which allows you to add payment requests to an account, and this account can
only ever have one pending payment at any one time.

My validator will check for pending payments on the account, if any exist
invalidate the form and produce a message to say pending payments are
already outstanding. This isn't specific to any element within the form, and
anywhere this form is used within my application I want this validation
check to be run, and error produced upon failure. So encapsulating it within
the form object I feel is the correct place to put it.

I've achieved exactly what I need by doing the following:

* extending the form's isValid() method
* adding the FormErrors decorator, so the error is spewed out
* injecting any fail messages from my validator into the form object using
addErrors($message)
* continuing with parent::isValid if my validator passes

Here is an example..

class App_Form_Payment extends Zend_Form
{

  .......... (construction stuff)........

  public function isValid( $value )
  {
    // add in a additional validator
    $paymentVal = new App_Form_Validators_SinglePayment();
    if (!$paymentVal->isValid($this->getPackageId()))
    {
      $this->addErrors($paymentVal->getMessages());
      return false;
    }
    return parent::isValid( $value );
  }

}

Adding custom errors to the form object is very easy and useful, you can
even flag the form as encountered an error without having to parse any
message information at all using the markAsError() method.

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?

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?


Lee Davis

--
View this message in context: 
http://zend-framework-community.634137.n4.nabble.com/Zend-Form-shifting-validation-off-the-element-onto-the-form-object-tp3436410p3436410.html
Sent from the Zend Framework mailing list archive at Nabble.com.

-- 
List: fw-general@lists.zend.com
Info: http://framework.zend.com/archives
Unsubscribe: fw-general-unsubscr...@lists.zend.com


Reply via email to