ANisus
ANisus

Reputation: 77955

Update Zend form element value in validator

I have a custom Zend Validate class extending Zend_Validate_Abstract
The validator is a file validator running an external command line tool that both validates and processes the file.

If the file is invalid, there is no problem. But if the file is valid, can I somehow edit the value of the Zend_Form_Element_File that the validator operates on with some of the information returned from the command line tool?

If not, what would be a good work-around considering I only want to run the external tool once?

Work around idea
I could create a Zend Filter that runs the command line tool, updating the value with parse info, including errors. Then I let the validator simply check if the value array contains the errors left there by the filter?

Upvotes: 0

Views: 879

Answers (1)

Sebastian Keßler
Sebastian Keßler

Reputation: 1053

Why not pass the element to the validator:

class CustomFileValidator extends Zend_Validate_Abstract {
  public $element = null;

  public function isValid($value) {
        //run external tool
        //check response, validity checks
        //...
        //modify the element if valid. e.g.:
        $this->element->setValue('');
  }

  public setElement(Zend_Form_Element $element) {
      $this->element = $element;
  }
}

Upvotes: 2

Related Questions