Remove html tags from Zend_Form
I want to get a simple HTML form:
<form me开发者_运维知识库thod="post">
<input type="text" name="question" id="question" value="" size="50%">
<input type="submit" name="ask" id="ask" value="Ask">
</form>
What I get with Zend Framework is:
<form enctype="application/x-www-form-urlencoded" method="post" action="">
<dt id="question-label"> </dt>
<dd id="question-element">
<input type="text" name="question" id="question" value="" size="50%"></dd>
<dt id="ask-label"> </dt><dd id="ask-element">
<input type="submit" name="ask" id="ask" value="Ask"></dd>
</form>
How can I remove unwanted html tags (dd, dt)?
I found similar question, and answer there lead me to using following solution (which works for me):
$form->setElementDecorators(array('ViewHelper','Errors'));
You can use ->removeDecorator(“DtDdWrapper”); to remove the dt and dd wrappers.
Full control over the output of a form without circumventing the Form Logic / filtering / validation
The View:
<form action="<?php echo $this->form->getAction(); ?>" method="<?php echo $this->form->getMethod(); ?>">
<!-- Errors For question field-->
<?php echo (NULL != ($errors = $this->form->getElement('question')->getMessages()) ? $this->formErrors($errors) : ''); ?>
<!-- question field -->
<?php echo $this->form->getElement('question')->renderViewHelper(); ?>
<!-- Submit Field -->
<?php echo $this->form->getElement('submit')->renderViewHelper(); ?>
</form>
End
The Form
<?php
class Form_Question extends Zend_Form
{
public function init()
{
$this->setMethod(self::METHOD_POST);
$element = $this->createElement('text', 'question');
$element->setLabel('Question');
$element->setRequired(TRUE);
$element->removeDecorator('DtDdWrapper');
$element->setAttrib('class', 'text');
$this->addElement($element);
$element = $this->createElement('submit', 'submit');
$element->setLabel('Submit');
$element->setRequired(TRUE);
$element->removeDecorator('DtDdWrapper');
$element->removeDecorator('label');
$this->addElement($element);
}
}
end form
the controller action
public function questionAction()
{
$form = new Form_Question();
if($this->getRequest()->isPost())
{
if($form->isValid($this->getRequest()->getPost()))
{
// Do stuff
}
else
{
$this->_helper->FlashMessenger(array('error' => "Errors! Correct the errors in the form below"));
}
}
$this->view->assign('form', $form);
}
end controller action
精彩评论