在ViewScript中显示Zend_Form表单错误
我正在尝试使用ViewScript在表单之前显示所有表单错误.这是我当前试图在ViewScript中使用的代码:
I'm trying to display all form errors before the form using a ViewScript. Here is the code that I'm currently trying to use within my ViewScript:
<div class="errors">
<?php echo $this->formErrors($this->element->getMessages()); ?>
</div>
此呼叫给我一条错误消息:
This call gives me an error message:
警告:htmlspecialchars()期望参数1为字符串,给定数组
Warning: htmlspecialchars() expects parameter 1 to be string, array given
我已经看到相同的代码在其他地方提出了建议,但是对我来说不起作用.如果我打印出$ this-> element-> getMessages(),我会看到以下错误消息:
I've seen this same code suggested other places but its not working for me. If I print out $this->element->getMessages() I do see the error messages as the following:
Array([myField] => Array([isEmpty] =>值是必需的,不能为空))
Array ( [myField] => Array ( [isEmpty] => Value is required and can't be empty ) )
有什么想法吗?
getMessages()返回一个由表单元素名称组成的数组作为键,每个键都包含该元素的错误数组.因此,基本上不用交给formErrors视图助手:
The getMessages() returns an array of form element names as keys which each contain an array of errors for that element. So basically instead of handing the formErrors view helper:
Array ( [isEmpty] => Value is required and can't be empty )
您正在处理它:
Array ( [myField] => Array ( [isEmpty] => Value is required and can't be empty ) )
您可能想要执行以下操作:
You would want to do something like this instead:
$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)
);
}