Sunday
21
September
2008

Zend_File_Transfer and error messages

Hy interested ones,

in the last days I worked on Zend_File_Transfer to get this new component more comfortable.
I integrated several new features which will allow simpler usage specially when you use the file element helper.

So what is new:

*) breakChainOnFailure

Like it’s cousine Zend_Form, now also Zend_File_Transfer allows the usage of the breakChainOnFailure parameter when adding validators. This feature will stop the validation for defined validators.

$adapter = new Zend_File_Transfer_Adapter_Http();
$adapter->addValidator('Size', true, '1MB')
            ->addValidator('Extension', false, 'jpg,png');

This example will stop validation as soon as a file extends 1MB regardless of it’s extension. So you will not get the error for false extension when the file is for example 1.5MB.

*) Translating error messages

You can now add a translator, which will translate all error messages for you. This feature works the same way like in Zend_Form. You can set a translation with setTranslator().

$adapter = new Zend_File_Transfer_Adapter_Http();
$adapter->addValidator('Size', true, '1MB')
            ->setTranslator($mytranslator);

When you now get an error when validation occurs, all returned error messages will be routed through the translator and when you have set a translation you will get your error instead of the adapters one.
So instead of “file not found” you could write “Hey man, give me my file”.

*) Knowing if a error has been occured

I added the hasErrors() method to the adapter which will return true when an error has occured during validation.

$adapter = new Zend_File_Transfer_Adapter_Http();
$adapter->addValidator('Size', true, '1MB');
	
if ($adapter->receive()) {
    $errors = 'unknown error';
    if ($adapter->hasErrors()) {
        $errors = $adapter->getErrors();
    }
}

The example does not really need the hasErrors() method as you may mention. But sometimes there is a need of this functionality, so I added it. Also the hasErrors() method of Zend_Form_Element_File routes to the adapters hasErrors() method.

*) Seperated validation from receiving of files

The originate implementation of Zend_Form_Element_File called receive() when the element was validated.
Of course this is not true and leaded to several problems, like the file being transfered even if other form validations failed.

This has been changed by me, but does also lead to a BC break, as you have to call receive() after validation.
But this does not mean that you have to call 2 methods.

$adapter = new Zend_File_Transfer_Adapter_Http();
$adapter->addValidator('Size', true, '1MB');
	
if (!$adapter->isValid()) {
    print "Validation error";
}
if (!$adapter->receive()) {
    print "Problem receiving the file";
}

When the adapter has been validated, then the receive() method will not call isValid() again.
But when you have not validated the adapter, it will automatically call isValid() before the file will be transfered to it’s destination.

So you could simply write

$adapter = new Zend_File_Transfer_Adapter_Http();
$adapter->addValidator('Size', true, '1MB');
	
if (!$adapter->receive()) {
    print "Problem receiving the file, probably validation problem";
}

*) Checking of the destination exists

Previously you could set anything as destination path and would get an false as soon as you receive the file but you would not know why as the validation would have passed. So I added a check for existance of the destination path. You will now get an exception if the directory you set does not exist, or is not readable.

$adapter = new Zend_File_Transfer_Adapter_Http();
try {
    $adapter->setDestination('/path/does/not/exist');
} catch (Zend_File_Transfer_Exception $e) {
    print $e->getMessage();
}

More to come soon…

Greetings
Thomas
I18N Team Leader, Zend Framework

Back to top
  1. jfalvarez

    Sunday, September 21, 2008 - 23:19:47

    Great news, is on the ZF trunk ? Thx.

  2. skytraveller

    Monday, October 13, 2008 - 15:42:43

    Very usefull updates!!! That’s exactly what I need!!! Maybe a silly question: where can I download it?

  3. thomasw

    Monday, October 13, 2008 - 16:28:25

    Included in ZF 1.6.2, ZF 1.7 and newer releases.
    Both will be available in a few days.

  4. Ole

    Wednesday, October 15, 2008 - 14:22:48

    Thank you Thomas! I was having a hard time getting the validators working with 1.6.1, now that 1.6.2 is out, everything works as expected. Keep up the good work!