views:

60

answers:

2

Hi,

I have created this file at My/View/Helper/FormElement.php

<?php

abstract class My_View_Helper_FormElement extends Zend_View_Helper_FormElement
{

    protected function _getInfo($name, $value = null, $attribs = null,
        $options = null, $listsep = null
    ) {

        $info = parent::_getInfo($name, $value, $attribs, $options, $listsep);

        $info['id'] = 'My new ID';

        return $info;
    }
}

How can i get the normal form elements to use this instead?

Why i want this?

Say that i use the same form multiple times on a page, the 'id='-tag of the formelements will apear multiple times, this is not w3c-valid. So initially i want to prefix the id with the id of the form.

Any better ideas or ways to do this is much apreciated.

Update: Just realized it's the same problem with the decorators :( Don't think this is the right path i've taken.

+1  A: 

Create new form class extending Zend_Form and in the init() method use variable $ns to add prefix/suffix to all elements. You can set $ns variable through form constructor.

class Form_Test extends Zend_Form
{

protected $ns;

public function init()
{
    $this->setAttrib('id', $this->ns . 'testForm');

    $name = new Zend_Form_Element_Text('name');
    $name->setAttrib('id', $this->ns . 'name');
    $name->setLabel('Name: *')->setRequired(true);


    $submit = new Zend_Form_Element_Submit('submit');
    $submit->setAttrib('id', $this->ns . 'submitbutton');
    $submit->setLabel('Add')->setIgnore(true);

    $this->addElements(array($name, $submit));
}

public function setNs($data)
{
    $this->ns = $data;
}

}

In the controller or wherever you are calling this forms specify each form instance:

$form1 = new Form_Test(array('ns' => 'prefix1'));
$this->view->form1 = $form1;

$form2 = new Form_Test(array('ns' => 'prefix2'));
$this->view->form2 = $form2;

// Validation if calling from the controller
if ($form1->isValid()) ...
bas
Sound like a valid approach, but i managed to get around this using subForms/DisplayGroups.
Phliplip
In your setAttrib() $ns should be $this->ns me sphinks!
Phliplip
yeah, pure typo! Will correct my example! Tnx
bas
A: 

Using multiple instances of same forms on a page can be validated if used as subform.

SubForms prefix all id's with the name/identifier of the subform.

Phliplip