我有收藏的表格。我将验证附加到整个集合 - 我只想检查集合元素之间是否存在某些关系。
它很棒。在数据错误的情况下 - 表单不会传递" isValid()"测试
但有一个问题。 formElementErrors / getMessages没有返回任何内容。 我做错了什么?
我的表格:
class Form implements InputFilterProviderInterface {
/**
* @return array
*/
public function getInputFilterSpecification()
{
return [
[
'name' => 'legend',
'required' => true,
'allowEmpty' => false,
'validators' => [
['name' => 'Callback', 'options' => [
'messages' => [
\Zend\Validator\Callback::INVALID_VALUE => 'Wrong',
],
'callback' => function ($values, $context=[]) {
return false;
},
]],
]
],
];
}
public function init()
{
$this->add(
[
'name' => 'legend',
'type' => 'Zend\Form\Element\Collection',
'options' => [
'label' => 'Legenda',
'count' => 2,
'should_create_template' => true,
'allow_add' => true,
'template_placeholder' => '__placeholder__',
'target_element' => [
'type' => 'Narzedzie\Form\Legenda\LegendyOpcjeFieldset',
],
],
]
);
}
}
并查看:
$element = $NarzedzieForm->get('legend');
var_dump($element->getMessages()); // in case of error - empty array!
echo $this->formElementErrors($element); // in case of error - empty string
echo $this->formColleciton($element);
答案 0 :(得分:0)
也许您需要添加这两条消息?
'messages' => [
\Zend\Validator\Callback::INVALID_VALUE => 'Wrong VALUE',
\Zend\Validator\Callback::INVALID_CALLBACK => 'Wrong CALLBACK',
],
因为您只提供一个无效的回调消息,因此可能会被抑制?我希望它会回到默认状态。但是,所有这些验证器消息对我来说似乎有点愚蠢。
看起来你的回调中有一个错误,可能会抛出一个异常,并且可能会在try catch语句中的验证器中被捕获?
应该是?
function ($values, $context=[]) {
foreach ($values as $value) {
if ($value['el'] == '1') return false;
}
return true;
},
在$ value中,而不是foreach中数组的$ value?可能想用isset($ value [' el'])检查是否设置了密钥?