dtdfl62844 2010-10-21 12:25
浏览 10
已采纳

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

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);
}
  • 写回答

2条回答 默认 最新

  • dsce23640 2010-10-21 12:47
    关注

    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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有没有可以帮我搞一个微信建群链接,包括群名称和群资料群头像那种,不会让你白忙
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题