ZF-7835: Label-Decorator produces invalid id with Array-Notation/subform and numeric indices

Description

When using multiple elements in a subform with numeric names (ids) the label decorator will create an id like "14-label", buts thats not valid. Its also not possible to change this behavior, because its hardcoded in Decorator/Label.php:317

$decorator->setOptions(array('tag' => $tag,
  'id'  => $this->getElement()->getName() . '-label'));

In the input-tag of the subform-element the name of the subform itself is prepended. This should be also here.

Comments

This can be a solution: It prepends the Array name (if exists) before the id of the label.

Zend/Form/Decorator/Label.php:316-...

            $id = $this->getElement()->isArray();
            if (is_null($id)) {
                $id = $this->getElement()->getName();
            } else {
                $id = $id . '-' . $this->getElement()->getName();
            }
            $decorator->setOptions(array('tag' => $tag,
                                         'id'  =>  $id . '-label'));

VERIFIED. The current behavior produces invalid XHTML.

The right kind of thing to do is to generate the label $id not based on $element->getName but on getFullyQualifiedName, but the array notation uses brackets which are invalid values for id attributes.

@Jack Tanner Brackets can be replaced with dashes.

Hi Sebastian, can you give a short example? Has the subform a numeric name or the element?

Thanks!

Sorry, but was 3 years and 3 minor-versions ago...

But as far as I can see in my own repot: The element has the numeric name

$this->getElement()->getName()

Okay, I'll write an unit test.

Thanks for your very quick answer!

Here is an unit test:


/**
 * @group ZF-7835
 */
public function testRenderingWithSubForm()
{
    // Form
    $form = new Zend_Form(
        array(
             'view'                         => $this->getView(),
             'decorators'                   => array(
                 'FormElements',
             ),
             'disableLoadDefaultDecorators' => true,
        )
    );

    // Element
    $element = new Zend_Form_Element(
        '100',
        array(
             'label'      => 'Foo',
             'decorators' => array(
                 'ViewHelper',
                 array(
                     'Label',
                     array('tag' => 'div')
                 ),
             ),
        )
    );

    // SubForm
    $subform = new Zend_Form_SubForm(
        array(
             'decorators' => array(
                 'FormElements',
             ),
        )
    );
    $subform->addElement($element);
    $form->addSubForm($subform, 'bar');

    // Test
    $actual   = trim($form->render());
    $expected = '
' . 'Foo
' . PHP_EOL . PHP_EOL . ''; $this->assertEquals($expected, $actual); }

Result:


OK (21 tests, 40 assertions)

Process finished with exit code 0

The problem was fixed in a previous version.