简体   繁体   中英

ZF2 apigility - How can we validate collections in json data

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

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 stack overflow

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM