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).
I have the validation working properly, but I receive an empty array when I call $form->getMessages()
.
The messages aren't being set inside ZendFormFieldset::setMessages
because its attempting to find an element by the error message key. (In my example below 'invalidDate'
).
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"
]
}
}
Update
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" => "ApplicationValidatorStart"
)
)
);
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…