Zend Framework

Zend_Form_Element_File does not register errors nor display them

Details

  • Type: Improvement Improvement
  • Status: Resolved Resolved
  • Priority: Major Major
  • Resolution: Fixed
  • Affects Version/s: 1.7.5
  • Fix Version/s: 1.8.0
  • Component/s: Zend_Form
  • Labels:
    None

Description

When I add an error to a Zend_Form_Element_File element:

  • error is not registered error is registered, it is not returned by ::getErrors() but returned by ::getErrorMessages()
  • error is not outputed when element is rendered
    whereas same piece of code on non-file element (text / textarea / checkbox ...) works perfectly.

Example:

<?php
// note: $form points to a Zend_Form instance and 'attachment' is a Zend_Form_Element_File object
$form->attachment->addError('An error occured');
...
echo $form->attachment; 
?>

Actual result:

<dt><label for="attachment" class="optional">Attach file</label></dt>
<dd><input type="file" name="attachment" id="attachment" /></dd>

Expected result:

<dt><label for="attachment" class="optional">Attach file</label></dt>
<dd><input type="file" name="attachment" id="attachment" />
<ul class="errors"><li>An error occured</li></ul></dd>

Activity

Hide
Remy Damour added a comment -

<?= $form->attachment->renderErrors() ?>
does not work either (but works on non-file items)

Show
Remy Damour added a comment -
<?= $form->attachment->renderErrors() ?>
does not work either (but works on non-file items)
Hide
Thomas Weidner added a comment -

Please give a complete example of your element and form creation/validation.
Because errors are rendered and tested in the testbed and displayed in my browser.

Show
Thomas Weidner added a comment - Please give a complete example of your element and form creation/validation. Because errors are rendered and tested in the testbed and displayed in my browser.
Hide
Remy Damour added a comment -

I've created a very simple controller action that does only one thing, output a form with an error attached on File element.

Here is /application/controllers/IndexController.php:

<?php
require_once 'Zend/Controller/Action.php';

/**
 * Default controller, used for redirections only
 */
class IndexController extends Zend_Controller_Action
{
	/**
	 * Show upload form
	 */
	public function indexAction()
	{
		require_once 'Zend/Form.php';
		$form = new Zend_Form();
		$form->setAction('/file/upload')
				 ->setMethod('post');
		$attachment = $form->createElement('file', 'attachment')->setLabel('Upload a file')->addError('An error occured');
		$form->addElement($attachment);
		$this->view->form = $form;
	}
}

Here is /application/views/scripts/index/index.phtml:

<?php echo $this->form->render();

Actual Result:

<form enctype="application/x-www-form-urlencoded" action="/file/upload" method="post"><dl class="zend_form">
<dt><label for="attachment" class="optional">Upload a file</label></dt>
<dd>
<input type="file" name="attachment" id="attachment" /></dd></dl></form>	</body>
</html>

Expected Result: same code, but with error message as well

I hope I am not wrong, I've read the doc, and it looks like I using it correctly.
Regards, Remy

Show
Remy Damour added a comment - I've created a very simple controller action that does only one thing, output a form with an error attached on File element. Here is /application/controllers/IndexController.php:
<?php
require_once 'Zend/Controller/Action.php';

/**
 * Default controller, used for redirections only
 */
class IndexController extends Zend_Controller_Action
{
	/**
	 * Show upload form
	 */
	public function indexAction()
	{
		require_once 'Zend/Form.php';
		$form = new Zend_Form();
		$form->setAction('/file/upload')
				 ->setMethod('post');
		$attachment = $form->createElement('file', 'attachment')->setLabel('Upload a file')->addError('An error occured');
		$form->addElement($attachment);
		$this->view->form = $form;
	}
}
Here is /application/views/scripts/index/index.phtml:
<?php echo $this->form->render();
Actual Result:
<form enctype="application/x-www-form-urlencoded" action="/file/upload" method="post"><dl class="zend_form">
<dt><label for="attachment" class="optional">Upload a file</label></dt>
<dd>
<input type="file" name="attachment" id="attachment" /></dd></dl></form>	</body>
</html>
Expected Result: same code, but with error message as well I hope I am not wrong, I've read the doc, and it looks like I using it correctly. Regards, Remy
Hide
Thomas Weidner added a comment -

I see...
To describe this:

The file element is based on the file transfer adapter.
So also the error is thrown from the adapter.

When you create a new error type with addError, this is not supported by files because the adapter would not know where he should attach it. Files themself do not have a error, they are working with validators.

Also, when you add a new errortype where should it be attached
...at the first validator ? What when you delete this validator ?
...at all validators ? Same error would be displayed multiple times ?
...and when this works, only displaying the attached error ? Or also the file errors ?

I would solve this different, at the time you validate / receive the file as shown in several examples.

Btw:
renderErrors() works by me, but displays as described previously only file errors.

Show
Thomas Weidner added a comment - I see... To describe this: The file element is based on the file transfer adapter. So also the error is thrown from the adapter. When you create a new error type with addError, this is not supported by files because the adapter would not know where he should attach it. Files themself do not have a error, they are working with validators. Also, when you add a new errortype where should it be attached ...at the first validator ? What when you delete this validator ? ...at all validators ? Same error would be displayed multiple times ? ...and when this works, only displaying the attached error ? Or also the file errors ? I would solve this different, at the time you validate / receive the file as shown in several examples. Btw: renderErrors() works by me, but displays as described previously only file errors.
Hide
Thomas Weidner added a comment -

Changed type from bug to improvement.
Will be reviewed internally.

Show
Thomas Weidner added a comment - Changed type from bug to improvement. Will be reviewed internally.
Hide
Remy Damour added a comment -

If addError method is not used, it may be interesting for it to throw an exception.
Thanks for the explanation anyway.

Show
Remy Damour added a comment - If addError method is not used, it may be interesting for it to throw an exception. Thanks for the explanation anyway.
Hide
Thomas Weidner added a comment -

Integrated with r14233

Show
Thomas Weidner added a comment - Integrated with r14233

People

Vote (0)
Watch (1)

Dates

  • Created:
    Updated:
    Resolved: