views:

203

answers:

2

Hey,

I got two forms and they share some ids as two input fields are called 'title'.

Zend generates me a nice output like this:

<dl class="zend-form">
  <dt id="title-label">
    <label for="form1-title" class="required">Description</label>
  </dt>
  <dd id="title-element">
    <input name="form1[title]" id="form1-title" value="..." type="text">
  </dd>
</dl>

Now the problem is that the dt and the dd elements are named wrong (should be form1-title-lable as this is a sub form).

I also tried to change the element decorators:

$this->addElements( ... );
$this->setElementDecorators(array(
        'ViewHelper',
        'Errors',
        array(array('data' => 'HtmlTag'),array( 'tag' => 'dd', 'class' => 'element' )),
        array(array('data' => 'Label'),array( 'tag' => 'dt', class=> 'label' ))
  ));

However the outcome wasn't as expected.

A label was added to my submit button and the ids of the dt elements were still there.

How do you remove the id attributes?


Edit - Element declaration:

    $titel = new Zend_Form_Element_Text('title');
    $titel->setLabel( "Title" )
          ->addValidator('NotEmpty', true)
          ->addValidator('stringLength',true, array(0, 255 ))
          ->setRequired(true)
          ->addFilter("Alnum", array(true))
          ->addFilter('StringTrim');

    $this->addElement($titel);
A: 

Hi, you can create a custom Label decorator so you can modify the default render function.

class App_Form_Decorator_Label extends Zend_Form_Decorator_Label
{

    public function render()
    {
        // Insert here the render function of Zend_form_Decorator_Label but without the id decorator.
    }
}
Skelton
+1  A: 

It sounds more like the problem is in your subforms not prepending their names to the ID. If you solve that then you won't need to remove the IDs.

But, if you want to remove the ID from an element using the DtDdWrapper decorator, you can do something like this.

class Form_Foo extends Zend_Form_SubForm
{
    public function init()
    {

        $title = new Zend_Form_Element_Text('foo_title');
        $title->setLabel('Title');
        $title->removeDecorator('DtDdWrapper');
        $title->addDecorator(new Decorator_Foo());      
        $this->addElement($title);
    }
}

class Decorator_Foo extends Zend_Form_Decorator_DtDdWrapper
{
    public function render($content)
    {
        return '<dt>&nbsp;</dt>' .
               '<dd>' . $content . '</dd>';
    }
}

That should give you elements without an ID tag.

Inkspeak