Custom Zend_Form element disappearing after validation
I've cr开发者_运维知识库eated a custom form element that allows me to place text in an arbitrary location in my form:
<?php
class Plano_Form_Element_Note extends Zend_Form_Element_Xhtml
{
public $helper = 'formNote';
/**
* Default decorators
*
* @return void
*/
public function loadDefaultDecorators()
{
if ($this->loadDefaultDecoratorsIsDisabled()) {
return;
}
$decorators = $this->getDecorators();
if (empty($decorators)) {
$this->addDecorator('ViewHelper')
->addDecorator('Errors')
->addDecorator('Label')
->addDecorator(array('row' => 'HtmlTag'), array('tag' => 'div', 'class' => 'form-row clearfix'));
}
}
}
This works like a charm, but as soon as I hit $form->isValid() the element turns up empty and only the wrapper shows:
<div class="form-row clearfix"></div>
The elements are added using subforms (method in my form class below):
/**
* Setup form elements and generate subforms
*
* @return Event_Form_Feedback_Enter
*/
protected function setupForm()
{
$partMapper = new Event_Model_FeedbackPart_Mapper();
$parts = $partMapper->fetchByFeedbackId($this->getFeedback()->getId(), array('order ASC', 'id DESC'));
foreach ($parts as $part)
{
switch ($part->getType())
{
case Event_Model_FeedbackPart::TYPE_TEXT:
$subform = new Event_Form_Feedback_Enter_Text();
break;
case Event_Model_FeedbackPart::TYPE_QUESTION_OPEN:
$subform = new Event_Form_Feedback_Enter_Question();
break;
case Event_Model_FeedbackPart::TYPE_QUESTION_MC:
$subform = new Event_Form_Feedback_Enter_MultipleChoiceQuestion();
break;
}
$subform->setup($part);
$this->addSubForm($subform, 'part-' . $part->getId());
}
$this->addSubmit();
}
... and here is the element creation in the actual form class (Event_Form_Feedback_Enter_Text
):
protected function setupForm()
{
$element = new Plano_Form_Element_Note('description');
$element->setValue($this->getPart()->getDescription());
$this->addElement($element);
}
It turns out the validation was breaking for static form element. To solve this, I have overridden the isValid() method in my Plano_Form_Element_Note
class:
public function isValid($value)
{
return true;
}
An alternative approach for placing arbitrary static content into a form is the AnyMarkup decorator.
精彩评论