Zend_Form没有显示错误消息

时间:2012-06-05 17:08:04

标签: zend-framework zend-form

我有以下ZF表单元素。

    $this->addElement('text', 'price', array(
        'required'  => true,
        'label'     => 'Price:',
        'attribs'   => array(
            'title'     => 'Please enter the value of your artwork'),
        'filters'   => array('Currency'),
        'validators'    => array(
            array('NotEmpty', true, array(
                'messages' => array(
                    Zend_Validate_NotEmpty::IS_EMPTY =>
                        "You must enter your artworks price"))),
            array('Float', true, array(
                'messages'  => array(
                    Zend_Validate_Float::INVALID => 
                        "You must enter a valid price",
                    Zend_Validate_Float::NOT_FLOAT =>
                        "You must enter a valid price"))),
            array('GreaterThan', true, array(
                'min'   => 0.99,
                'messages'  => array(
                    Zend_Validate_GreaterThan::NOT_GREATER =>
                        "You must enter a value of £1.00 or more"))))
    ));

最后一个验证程序是Zend_Validate_GreaterThan,它已设置错误消息,我的问题是当此验证程序失败时,表单上不显示错误消息。我得到的所有内容都是空的无序列表!!

<ul>
    <li></li>
</ul>

当我检查zend_form输出的消息时,我收到错误和消息。

array(1) {
  ["price"]=>
  array(1) {
    ["notGreaterThan"]=>
    string(39) "You must enter a value of £1.00 or more"
  }
}

有谁知道为什么邮件没有在表单上呈现?

非常感谢提前

加里

修改

我正在使用的唯一装饰器是一个用于在我的表单上呈现表单的视图。

$this->setDecorators(array(
    array('ViewScript', array('viewScript' => 'forms/add-item.phtml'))
));

和viewscript本身。

$attribFilterObj = new Freedom_Zend_Filter_HtmlAttribs();
$attribs = $attribFilterObj->filter($this->element->getAttribs());
?>
<form <?php echo $attribs; ?>>
<dl>
    <?php echo $this->element->ArtworkTitle->title->render(); ?>
    <?php echo $this->element->ArtworkDescription->description->render(); ?>
    <?php echo $this->element->price->render(); ?>
    <?php echo $this->element->Genres->render(); ?>
    <?php echo $this->element->image->render(); ?>
    <?php echo $this->element->add->render(); ?>
</dl>
</form>

修改

var_dump的输出如下

array(5) {
  ["Zend_Form_Decorator_ViewHelper"]=>
  object(Zend_Form_Decorator_ViewHelper)#157 (6) {
    ["_buttonTypes":protected]=>
    array(3) {
      [0]=>
      string(24) "Zend_Form_Element_Button"
      [1]=>
      string(23) "Zend_Form_Element_Reset"
      [2]=>
      string(24) "Zend_Form_Element_Submit"
    }
    ["_helper":protected]=>
    NULL
    ["_placement":protected]=>
    string(6) "APPEND"
    ["_element":protected]=>
    NULL
    ["_options":protected]=>
    array(0) {
    }
    ["_separator":protected]=>
    string(2) "
"
  }
  ["Zend_Form_Decorator_Errors"]=>
  object(Zend_Form_Decorator_Errors)#158 (4) {
    ["_placement":protected]=>
    string(6) "APPEND"
    ["_element":protected]=>
    NULL
    ["_options":protected]=>
    array(0) {
    }
    ["_separator":protected]=>
    string(2) "
"
  }
  ["Zend_Form_Decorator_Description"]=>
  object(Zend_Form_Decorator_Description)#159 (6) {
    ["_escape":protected]=>
    NULL
    ["_placement":protected]=>
    string(6) "APPEND"
    ["_tag":protected]=>
    NULL
    ["_element":protected]=>
    NULL
    ["_options":protected]=>
    array(2) {
      ["tag"]=>
      string(1) "p"
      ["class"]=>
      string(11) "description"
    }
    ["_separator":protected]=>
    string(2) "
"
  }
  ["Zend_Form_Decorator_HtmlTag"]=>
  object(Zend_Form_Decorator_HtmlTag)#160 (7) {
    ["_encoding":protected]=>
    NULL
    ["_placement":protected]=>
    NULL
    ["_tag":protected]=>
    NULL
    ["_tagFilter":protected]=>
    NULL
    ["_element":protected]=>
    NULL
    ["_options":protected]=>
    array(2) {
      ["tag"]=>
      string(2) "dd"
      ["id"]=>
      array(1) {
        ["callback"]=>
        array(2) {
          [0]=>
          string(22) "Zend_Form_Element_Text"
          [1]=>
          string(16) "resolveElementId"
        }
      }
    }
    ["_separator":protected]=>
    string(2) "
"
  }
  ["Zend_Form_Decorator_Label"]=>
  object(Zend_Form_Decorator_Label)#161 (6) {
    ["_placement":protected]=>
    string(7) "PREPEND"
    ["_tag":protected]=>
    NULL
    ["_tagClass":protected]=>
    NULL
    ["_element":protected]=>
    NULL
    ["_options":protected]=>
    array(1) {
      ["tag"]=>
      string(2) "dt"
    }
    ["_separator":protected]=>
    string(2) "
"
  }
}

3 个答案:

答案 0 :(得分:1)

这有时是由于忘记包含“错误”而造成的。在元素的表单装饰器中。如果您正在使用自定义装饰器,请先检查一下。

答案 1 :(得分:1)

有点旧但请查看此网站http://zendguru.wordpress.com/2008/12/04/handling-zend-framework-form-error-messages/

我相信你必须得到你喜欢的错误信息,然后打印出来

$errorsMessages = $this->form->getMessages();

答案 2 :(得分:0)

我遇到了同样的问题,我只是像你一样使用ViewScript。这解决了它:

($this is in the form class I created)
$this->setDecorators(array(
        array('ViewScript', 
              array('viewScript' => '/formElementViewscripts/editRoughDraft/_form.phtml')),
        'Form'
    ));

如果您希望能够接收这些消息,还必须将默认的“表单”装饰器添加到表单中。我尝试查看代码,但我无法明确指出原因。