ZF2 apigility - How can we validate collections in json data

北慕城南 提交于 2019-12-18 13:38:17

问题


How can I get validated json value using Apigility. For example, I need to get validated user_id under users collection in the following json data.

{   
    "log_type": "split food",   
    "meal_type": "Break Fast",  
    "meal_date": "12-2-2015",   
    "users": [
        {
            "user_id": 1,
            "food_details": [
                {
                   "food_id":101
                }
            ]
        }
    ] 
}

I know fields can be validated through apigility but here is from json.

Thank you


回答1:


You should look into the documentation of ZF2 validation for validating (form) collections. Some documentation on this can be found here. You should set the type field like this:

'type' => 'Zend\InputFilter\CollectionInputFilter',

for validation of nested objects (or form field sets) you need to set the type field as follows:

'type' => 'Zend\InputFilter\InputFilter'

You use it like this:

'input_filter' => array(                
    'log_type' => array(
        'validators' => array(
            // ... validators ...
        ),
        'filters' => array(
            // ... filters ...
        ),
     ),
    'meal_type' => array(
        'validators' => array(
            // ... validators ...
        ),
        'filters' => array(
            // ... filters ...
        ),
     ),
     'meal_date' => array(
        'validators' => array(
            // ... validators ...
        ),
        'filters' => array(
            // ... filters ...
        ),
     ),
    'users' => array(
        'required' => true,
        'count' => ... optional count ...
        'input_filter' => ... input filter or input filter config to use for each element ...
        'type' => 'Zend\InputFilter\CollectionInputFilter',
    ),
    'some_complex_element' => array(
        'property_of_complex_element' => array(
            'name' => 'property_of_complex_element',
            'required' => false,
            'validators' => array(
                // ... validators ...
            ),
            'filters' => array(
                // ... filters ...
            ),
        ),
        'type' => 'Zend\InputFilter\InputFilter',
     )          
),

An example on how to use this can be found here on stackoverflow

To achieve what you want you most likely have to combine those two solutions. Not sure if it is the easiest way to do it, but it is definitely possible!

EDIT

For people who haven't setup validation at all yet:

For content validation in Apigility You have to use the zfcampus/zf-content-validation module and follow the documentation for configuration. This module allows you to configure your input-filters and validators in a input_filter_spec like you would normally do for form validation in ZF2. Here inside these input-filter config arrays you can use the configs that I referenced above.

So first properly install that module and once set-up you will be able to use these validation types in Apigility.



来源:https://stackoverflow.com/questions/28668444/zf2-apigility-how-can-we-validate-collections-in-json-data

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