Reputation: 547
Here is my code
private $elementDecorators = array( 'ViewHelper', 'Errors', array(array('data' => 'HtmlTag'), array('tag' => 'td')), array('Label', array('tag' => 'td','class'=>'blue-color','placement'=>'prepend')), array(array('row' => 'HtmlTag'), array('tag' => 'tr')), ); public function init() { $username = new Zend_Form_Element_Text('username',array( 'decorators' =>$this->elementDecorators, 'label' =>'Username', 'required' =>true, 'span' =>array('class'=>'validation','id'=>'unameInfo'), )); } $this->addElements(array( $username )); $this->setDecorators(array( 'FormElements', array('HtmlTag', array('tag'=>'table', 'width' => '100%') ), 'Form' ));
Form created for above code is as below
<tr>
<td id="username-label"><label for="username" class="blue-color required">Username</label></td>
<td><input type="text" name="username" id="username" value="" span="Array"></td>
</tr>
I want following html
<tr>
<td id="username-label"><label for="username" class="blue-color required">Username</label></td>
<td>
<input type="text" name="username" id="username" value="" span="Array">
<span class="validation" id="userinfo"></span>
</td>
</tr>
How can i add span tag in my above zend form code?
Thank you in advance
Upvotes: 2
Views: 1951
Reputation: 8228
Try like below,
...
$submit = new Zend_Form_Element_Submit('submit', array(
'label' => 'Submit Button',
'class' => 'form-submit',
'decorators' => array(
'ViewHelper',
),
));
//$submit->removeDecorator('Label');
$this->addElement($submit);
$reset = new Zend_Form_Element_Reset('reset', array(
'label' => 'Reset Button',
'class' => 'form-reset',
'decorators' => array(
'ViewHelper',
),
));
//$submit->removeDecorator('Label');
$this->addElement($reset);
$this->addDisplayGroup(array('submit', 'reset',), 'submitButtons', array(
'order' => 10,
'decorators' => array(
'FormElements',
array(
array('data' => 'HtmlTag'),
array('tag' => 'td','class'=>'move_td')
),
array(
array('row' => 'HtmlTag', 'class' => 'element'),
array('tag' => 'tr')
)
),
));
...
This will generate code like below,
...
<td class="move_td">
<input type="submit" class="form-submit" value="Submit Button" id="submit" name="submit">
<input type="reset" class="form-reset" value="Reset Button" id="reset" name="reset">
</td>
...
Updated
Use below code to generate span
$this->addElement(
'hidden',
'dummy',
array(
'required' => false,
'ignore' => true,
'autoInsertNotEmptyValidator' => false,
'decorators' => array(
array(
'HtmlTag', array(
'tag' => 'span',
'id' => 'span-id',
'class' => 'span-class'
)
)
)
)
);
$this->dummy->clearValidators();
Now you need to use addDisplayGroup function to group html elements.
Upvotes: 0
Reputation: 14184
You can use the AnyMarkup decorator.
$username = new Zend_Form_Element_Text('username',array(
'decorators' => array(
'ViewHelper',
array('AnyMarkup', array('markup' => 'your-markup-here', 'placement' => 'append')),
'Errors',
array(array('data' => 'HtmlTag'), array('tag' => 'td')),
array('Label', array('tag' => 'td','class'=>'blue-color','placement'=>'prepend')),
array(array('row' => 'HtmlTag'), array('tag' => 'tr')),
),
'label' => 'Username',
'required' => true,
'span' => array('class'=>'validation','id'=>'unameInfo'),
// actually, this last 'span' entry strikes me as odd
));
To add the decorator to an element using the short-form (as above) rather than creating an instance, you need to register the decorator's path/prefix with the element, something like:
$username->addPrefixPath('My_Decorator_', APPLICATION_PATH . '/../library/My/Decorator', Zend_Form_Element::DECORATOR);
You can add that prefix/path to all (currently defined) elements using the:
$form->addElementPrefixPath($prefix, $path)
method.
Upvotes: 0