views:

22

answers:

2

I've created 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);
}

I am most likely overlooking something silly, but at this point I do not see it. Any help is appreciated.

+1  A: 

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;
}
Aron Rotteveel
+1  A: 

An alternative approach for placing arbitrary static content into a form is the AnyMarkup decorator.

David Weinraub