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


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.


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


            $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?


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


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(
             'view'                         => $this->getView(),
             'decorators'                   => array(
             'disableLoadDefaultDecorators' => true,

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

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

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


OK (21 tests, 40 assertions)

Process finished with exit code 0

The problem was fixed in a previous version.