我正在尝试使用 ViewScript 在表单之前显示所有表单错误。这是我目前尝试在我的 ViewScript 中使用的代码:
<div class="errors">
<?php echo $this->formErrors($this->element->getMessages()); ?>
</div>
此调用给了我一条错误消息:
Warning: htmlspecialchars() expects parameter 1 to be string, array given
我已经看到同样的代码建议其他地方,但它对我不起作用。如果我打印出 $this->element->getMessages() 我确实会看到如下错误消息:
Array ( [myField] => Array ( [isEmpty] => Value is required and can't be empty ) )
有任何想法吗?
最佳答案
getMessages() 返回一个表单元素名称数组作为键,每个键都包含该元素的错误数组。所以基本上不是处理 formErrors View 助手:
Array ( [isEmpty] => Value is required and can't be empty )
你递给它:
Array ( [myField] => Array ( [isEmpty] => Value is required and can't be empty ) )
你会想要做这样的事情:
$arrMessages = $this->myForm->getMessages();
foreach($arrMessages as $field => $arrErrors) {
echo sprintf(
'<ul><li>%s</li>%s</ul>',
$this->myForm->getElement($field)->getLabel(),
$this->formErrors($arrErrors)
);
}
关于zend-framework - 在 ViewScript 中显示 Zend_Form 表单错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3454401/