Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
824 views
in Technique[技术] by (71.8m points)

php - ZF2 Collection Validation

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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' => 'ZendInputFilterInputFilter'
    ),
    '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' => 'ZendInputFilterInputFilter'
    )
)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...