ZF2集合验证 [英] ZF2 Collection Validation
问题描述
是否可以将错误消息附加到Fieldset本身,而不是ZF2中的子元素?我有一个包含两个Fieldsets的表单,我需要确保Fieldset1中填充的元素也填充在Fieldset2中. (每个字段集中都有可选元素,但是如果填写Fieldset1->element1
,则需要填写Fieldset2->element1
).
Is it possible to attach error messages to the Fieldset itself and not a child element in ZF2? I have a form with two Fieldsets and I need ensure the elements that are filled in Fieldset1 are also filled in Fieldset2. (There are optional elements inside each fieldset, but if Fieldset1->element1
is filled in, Fieldset2->element1
needs to be filled in).
我的验证工作正常,但是调用$form->getMessages()
时收到一个空数组.
I have the validation working properly, but I receive an empty array when I call $form->getMessages()
.
消息未设置在Zend\Form\Fieldset::setMessages
内部
因为它试图通过错误消息键来查找元素. (在下面的示例中,'invalidDate'
).
The messages aren't being set inside Zend\Form\Fieldset::setMessages
because its attempting to find an element by the error message key. (In my example below 'invalidDate'
).
我正在尝试向Fieldset本身添加错误消息,因为错误不仅限于一个特定的字段,而是整个集合.
I am attempting to add an error message to the Fieldset itself, because the error is not limited to one particular field, but the collection as a whole.
//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
包含年,月,周,日等元素.
This is the validation for the start
fieldset. The validation works, I can compare the start
and end
fieldsets with the context param. start
and end
contain elements such as year, month, week, day, etc.
return array(
"name" => "start",
"required" => true,
"validators" => array(
array(
"name" => "Application\Validator\Start"
)
)
);
推荐答案
您可以通过制作嵌套的输入过滤器(每个字段集一个输入过滤器配置,来解决此类字段集.我在配置中为年份显示了一个验证器,向您展示了如何可以工作:
You can solve such fieldsets by making nested input filters (an input filter config for each fieldset. I showed one validator for year in the config to show you how this can work:
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'
)
)
这篇关于ZF2集合验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!