开发者

Zend Decorators - Remove Id Field for DT Wrapper

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开发者_C百科 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);


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.


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.
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜