1

我有以下 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'))
));

和视图本身。

$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) "
"
  }
}
4

3 回答 3

1

有点旧,但检查这个网站http://zendguru.wordpress.com/2008/12/04/handling-zend-framework-form-error-messages/

我相信你必须自己得到错误信息,然后打印出来

$errorsMessages = $this->form->getMessages();
于 2012-06-09T13:49:53.807 回答
1

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

于 2012-06-06T13:10:20.923 回答
0

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

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

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

于 2013-06-01T02:31:34.550 回答