验证后,自定义Zend_Form元素消失

时间:2010-10-21 12:25:27

标签: php forms zend-framework

我创建了一个自定义表单元素,允许我将文本放在表单中的任意位置:

<?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'));
        }
    }       
}

这就像魅力一样,但只要我点击$ form-&gt; isValid(),元素就会变空,只有包装器显示

<div class="form-row clearfix"></div>

使用子表单(下面的表单类中的方法)添加元素:

/**
 * 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();
}

...这里是实际表单类(Event_Form_Feedback_Enter_Text)中的元素创建:

protected function setupForm()
{
    $element = new Plano_Form_Element_Note('description');
    $element->setValue($this->getPart()->getDescription());
    $this->addElement($element);
}

2 个答案:

答案 0 :(得分:3)

事实证明,静态表单元素的验证正在破坏。为了解决这个问题,我在Plano_Form_Element_Note类中重写了isValid()方法:

public function isValid($value)
{
    return true;
}

答案 1 :(得分:1)

将任意静态内容放入表单的另一种方法是AnyMarkup decorator