簡體   English   中英

ZF2集合驗證

[英]ZF2 Collection Validation

是否可以將錯誤消息附加到Fieldset本身,而不是ZF2中的子元素? 我有一個包含兩個Fieldsets的表單,我需要確保Fieldset1中填充的元素也填充在Fieldset2中。 (有每個字段集內的可選元素,但如果Fieldset1->element1被填充在, Fieldset2->element1需要被填充)。

我的驗證工作正常,但是當我調用$form->getMessages()時收到一個空數組。

沒有在Zend\\Form\\Fieldset::setMessages內部設置消息,因為它試圖通過錯誤消息鍵來查找元素。 (在我的示例中'invalidDate'下方)。

我試圖將錯誤消息添加到Fieldset本身,因為該錯誤不僅限於一個特定的字段,而是整個集合。

//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字段集的驗證。 驗證有效,我可以將startend字段集與上下文參數進行比較。 startend包含年,月,周,日等元素。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM