是否可以将错误消息附加到Fieldset本身而不是ZF2中的子元素?我有一个包含两个Fieldset的表单,我需要确保在Fieldset2中填充的元素也填充在Fieldset2中。 (每个字段集中都有可选元素,但如果填写Fieldset1->element1
,则需要填写Fieldset2->element1
。
我的验证工作正常,但是当我拨打$form->getMessages()
时,我收到一个空数组。
消息未在Zend\Form\Fieldset::setMessages
内设置
因为它试图通过错误消息键找到一个元素。 (在我的示例'invalidDate'
)中。
我正在尝试向Fieldset本身添加错误消息,因为错误不仅限于一个特定字段,而是整个集合。
//Regular Error
{
start: {
year: [
regexInvalid: "SomeMessage"
]
},
end: {
year: [
regexInvalid: "SomeMessage"
]
}
}
//Fieldset level Error
{
start: {
invalidDate: [
noMatch: "Filled in values of 'start' and 'end' must match"
]
},
end: {
invalidDate: [
noMatch: "Filled in values of 'start' and 'end' must match"
]
}
}
更新
这是start
字段集的验证。验证有效,我可以将start
和end
字段集与上下文参数进行比较。 start
和end
包含年,月,周,日等元素。
return array(
"name" => "start",
"required" => true,
"validators" => array(
array(
"name" => "Application\Validator\Start"
)
)
);
答案 0 :(得分:0)
您可以通过嵌套输入过滤器(每个字段集的输入过滤器配置)来解决此类字段集。我在配置中显示了一年的一个验证器,以向您展示它如何工作:
array(
'start' => array(
'day' => array(
'name' => 'end',
'required' => false
),
'week' => array(
'name' => 'end',
'required' => false
),
'month' => array(
'name' => 'end',
'required' => false
),
'year' => array(
'name' => 'end',
'required' => false
),
// type key necessary for nested input filter
'type' => 'Zend\InputFilter\InputFilter'
),
'end' => array(
'day' => array(
'name' => 'end',
'required' => false
),
'week' => array(
'name' => 'end',
'required' => false
),
'month' => array(
'name' => 'end',
'required' => false
),
'year' => array(
'name' => 'end',
'required' => false,
'filters' => array(),
'validators' => array(
array(
'name' => 'Callback',
'options' => array(
'messages' => array(
Callback::INVALID_VALUE => "Filled in values of start year and end year must match",
),
'callback' => function($value, $context = array()) {
// value of end
$endYear = $value;
// value of start year
$startYear = $context['start']['year'];
// validate
return $endYear >= $startYear;
}
)
)
)
),
// type key necessary for nested input filter
'type' => 'Zend\InputFilter\InputFilter'
)
)