ZF-12116: Zend_Form_Element_File NotEmpty validator leaking into markup

Description

Zend_Form_Element_File is leaking configurations into form element markup. An example will better illustrate:

Expected markup:



Actual markup: 

To reproduce the actual markup above:

  • Form file element must be required
  • form need to be processed through isValid()


class Bug_Form_FileRequriedComponentMarkupLeak {
    public function init() {
        $this->addElement('file', 'uploadElement', array(
            'label'         => 'Label text',
            'required'      => true
        ));
        // ...
    }
}

$form = new Bug_Form_FileRequriedComponentMarkupLeak();

if ($form->isValid($_POST) {
    echo $form; // e.g. from view script
}

Comments

I can not reproduce the problem.

Here is an unit test:


/**
 * @group ZF-12116
 */
public function testRenderingAfterSubmit()
{
    $this->element->setRequired(true);
    $this->element->setDecorators(array('File'));
    $this->element->setMaxFileSize(0);

    $this->element->isValid('foo.jpg');

    $this->assertEquals(
        '',
        trim($this->element->render(new Zend_View()))
    );
}

Result: OK

I ran that test and it failed!


PHPUnit 3.5.15 by Sebastian Bergmann.

FF

Time: 1 second, Memory: 6.75Mb

There were 2 failures:

1) Warning
No tests found in class "IndexControllerTest".


2) formTest::testRenderingAfterSubmit
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-
+

/path/to/bug-ZF-12116/tests/application/forms/formTest.php:20

FAILURES!
Tests: 2, Assertions: 1, Failures: 2.

ZF Version: 1.12.1

Hi Gerard, thanks for your feedback. I ran the test on the current trunk, but I will check this again.

Thanks for your help!

No worries, maybe something was fixed in trunk.

Here's how I tested, it might help.


$ mkdir bug-zf-12116
$ cd bug-zf-12116
$ zf create project .

    Creating project at /path/to/bug-zf-12116
    Note: This command created a web project, for more information setting up your VHOST, please see docs/README
    Testing Note: PHPUnit was not found in your include_path, therefore no testing actions will be created.

$ phpunit -c tests/phpunit.xml

    PHPUnit 3.5.15 by Sebastian Bergmann.

    F

    Time: 0 seconds, Memory: 4.25Mb

    There was 1 failure:

    1) Warning
    No tests found in class "IndexControllerTest".


    FAILURES!
    Tests: 1, Assertions: 0, Failures: 1.

$ mkdir tests/application/forms
$ vi tests/application/forms/fileElementTest.php

    <?php

    class fileElementTest extends PHPUnit_Framework_TestCase
    {
        public function testRenderingAfterSubmit()
        {
            $element = new Zend_Form_Element_File('foo');
            $element->setRequired(true);
            $element->setDecorators(array('File'));
            $element->setMaxFileSize(0);

            $element->isValid('foo.jpg');

            $this->assertEquals(
                '',
                trim($element->render(new Zend_View()))
            );
        }
    }

$ phpunit -c tests/phpunit.xml

    PHPUnit 3.5.15 by Sebastian Bergmann.

    FF

    Time: 0 seconds, Memory: 6.75Mb

    There were 2 failures:

    1) Warning
    No tests found in class "IndexControllerTest".


    2) fileElementTest::testRenderingAfterSubmit
    Failed asserting that two strings are equal.
    --- Expected
    +++ Actual
    @@ @@
    -
    +

    /path/to/bug-zf-12116/tests/application/forms/fileElementTest.php:17

    FAILURES!
    Tests: 2, Assertions: 1, Failures: 2.


Yes, it was fixed with: ZF-12210