ZF2 Collection Validation

余生颓废 提交于 2019-12-11 23:29:38

问题


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:


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


来源:https://stackoverflow.com/questions/34167386/zf2-collection-validation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!