Php ZF2集合验证

Php ZF2集合验证,php,zend-framework2,formcollection,Php,Zend Framework2,Formcollection,是否可以将错误消息附加到字段集本身而不是ZF2中的子元素?我有一个包含两个字段集的表单,我需要确保在Fieldset1中填写的元素也在Fieldset2中填写。(每个字段集中都有可选元素,但如果填写了Fieldset1->element1,则需要填写Fieldset2->element1) 我的验证工作正常,但调用$form->getMessages()时收到一个空数组 未在Zend\Form\Fieldset::setMessages中设置消息 因为它试图通过错误消息键查找元素。(在我下面的示

是否可以将错误消息附加到字段集本身而不是ZF2中的子元素?我有一个包含两个字段集的表单,我需要确保在Fieldset1中填写的元素也在Fieldset2中填写。(每个字段集中都有可选元素,但如果填写了
Fieldset1->element1
,则需要填写
Fieldset2->element1

我的验证工作正常,但调用
$form->getMessages()
时收到一个空数组

未在
Zend\Form\Fieldset::setMessages中设置消息
因为它试图通过错误消息键查找元素。(在我下面的示例中,
'invalidDate'

我试图向字段集本身添加一条错误消息,因为错误不限于一个特定字段,而是整个集合

//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
字段集与上下文参数进行比较<代码>开始
结束
包含年、月、周、日等元素

return array(
    "name" => "start",
    "required" => true,
    "validators" => array(
        array(
            "name" => "Application\Validator\Start"
        )
    )
);

您可以通过创建嵌套的输入过滤器(每个字段集的输入过滤器配置)来解决此类字段集。我在配置中显示了一个全年的验证器,以向您展示这是如何工作的:

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'
    )
)

感谢您的回复。Start和End是字段集,不是元素。我已经完成了验证,但是调用getMessages()时不会返回消息,因为Start和End不是实际的元素。