dongze5043 2015-12-08 22:34
浏览 35

ZF2收集验证

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 Zend\Form\Fieldset::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" => "Application\Validator\Start"
        )
    )
);
  • 写回答

1条回答 默认 最新

  • doushi7394 2015-12-09 08:46
    关注

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

报告相同问题?

悬赏问题

  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 vue3加ant-design-vue无法渲染出页面
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏