0
votes

In my project we have a Form where the user either needs to upload a file or specify some other information, for example in a text input.

I know that there is the possibility to attach a Count validator to the Form Element, and this works like this:

$upload->addValidator('Count', false, array('min' => 1, 'max' => 3));

This validator gets called, no matter if there are files uploaded or not. But as soon as i replace the validator with a custom version, and do not upload any files, the validator does not get called.

Looking at the isValid() method of Zend_File_Transfer_Adapter_Abstract, I can see the problem:

[...]
    foreach($check as $key => $content) {
        if (array_key_exists('validators', $content) &&
            in_array('Zend_Validate_File_Count', $content['validators'])) {
            $validator = $this->_validators['Zend_Validate_File_Count'];
            $count     = $content;
[...]

Seems like the Count Validator got some special treatment here. But how can I include my customized Validator, without overwriting Zend Files?

Because we do not extend the Zend_Form class, sadly I also cannot overwrite the isValid() method of the involved Form. Any Ideas left? Thanks for your time!

1
not sure what the question is, maybe some example code could help? - RockyFord

1 Answers

1
votes

This would probably be easiest with a bit of ajax. That way you could evaluate the selection prior to posting the form.
To do it in PHP, an Element validator is probably not the way to go. It would be better to handle the selection with an if() statment.

pseudocode...
//if file element contains something and text doesn't
if($this->getRequest->getPost('file'=='' && 'text' != ''){
    $file = $form->file->receive();

    //do some stuff
//if text element contains something and file dosen't
}elseif ($this->getRequst->getPost('file' != '' && 'text' == '') {
    if($form->isValid($this->_request->getPost('text')) {
      //do some stuff
    }
}

This is the idea, hope it helps.