简体   繁体   English

ZF2 apigility - 我们如何验证json数据中的集合

[英]ZF2 apigility - How can we validate collections in json data

How can I get validated json value using Apigility. 如何使用Apigility获得验证的json值。 For example, I need to get validated user_id under users collection in the following json data. 例如,我需要在以下json数据中的users集合下获得验证的user_id

{   
    "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. 我知道字段可以通过apigility验证,但这里是来自json。

Thank you 谢谢

You should look into the documentation of ZF2 validation for validating (form) collections. 您应该查看用于验证(表单)集合的ZF2验证文档。 Some documentation on this can be found here . 有关此的一些文档可以在这里找到 You should set the type field like this: 你应该像这样设置type字段:

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

for validation of nested objects (or form field sets) you need to set the type field as follows: 要验证嵌套对象(或表单字段集),您需要设置type字段,如下所示:

'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. 对于Apigility内容验证您必须使用zfcampus/zf-content-validation模块并按照文档进行配置。 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. 此模块允许您在input_filter_spec配置输入过滤器和验证器,就像在ZF2中通常用于表单验证一样。 Here inside these input-filter config arrays you can use the configs that I referenced above. 在这些input-filter配置数组中,您可以使用我在上面引用的配置。

So first properly install that module and once set-up you will be able to use these validation types in Apigility . 因此,首先正确安装该模块,一旦设置,您将能够在Apigility使用这些验证类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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