我在Zend 2中有一个简单的表单,当发生验证错误时,我得到一个无序的错误列表 - 如何在当前代码的限制内向ul标记添加css类?理想情况下,我希望在网站上做这个,所以重复代码最少...
<?php
namespace UserManagement\Form;
use Zend\Form\Form;
use Zend\InputFilter\InputFilterProviderInterface;
class SearchUserForm extends Form implements InputFilterProviderInterface
{
public function __construct($name = null)
{
// we want to ignore the name passed
parent::__construct('SearchUser');
$this->setAttribute('method', 'post');
$this->add(array(
'name' => 'search',
'attributes' => array(
'type' => 'text',
'required' => true,
)
));
$this->add(array(
'name' => 'submit',
'attributes' => array(
'type' => 'submit',
'value' => 'Go',
'id' => 'submitbutton',
),
));
}
public function getInputFilterSpecification()
{
return [
'search' => [
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 4,
'max' => 100,
),
)
),
]
];
}
在视图中:
<div class="search">
<label for="search">Search: </label>
<?php
echo $this->form()->openTag($searchForm);
echo $this->formRow($searchForm->get('search'));
echo $this->formSubmit($searchForm->get('submit'));
echo $this->form()->closeTag();
?>
</div>
目前输出的错误是:
<ul>
<li>The input is less than 4 characters long</li>
</ul>
我想:
<ul class='validation-errors'>
<li>The input is less than 4 characters long</li>
</ul>
答案 0 :(得分:1)
而不是使用formRow
视图助手输出标签,元素和错误 - 您可以使用各自的视图助手输出每个。然后,formElementErrors
视图助手将允许您使用指定的css类包装错误,例如:
echo $this->formElementErrors($element, array('class' => 'help-inline'));
以上示例摘自官方文档:http://framework.zend.com/manual/2.3/en/modules/zend.form.view.helpers.html#formelementerrors
答案 1 :(得分:0)
您可以在视图脚本中配置formElementErrors视图助手。
$this->formElementErrors()
->setMessageOpenFormat('<ul class="validation-errors"><li>')
->setMessageSeparatorString('</li><li>')
->setMessageCloseString('</li></ul>');
它适用于视图助手,如formElementErrors(),formRow()和formCollection()。