ZF2 apigility - 我们如何验证json数据中的集合 [英] ZF2 apigility - How can we validate collections in json data

查看:234
本文介绍了ZF2 apigility - 我们如何验证json数据中的集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Apigility获得有效的json值。例如,我需要在以下json数据中的 users 下验证 user_id

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
                }
            ]
        }
    ] 
}

我知道字段可以通过apigility验证,但这里是从json。

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

谢谢

推荐答案

您应该查看用于验证(表单)集合的ZF2验证的文档。 有关这方面的一些文档在这里找到
应该这样设置类型字段:

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



<您需要设置类型字段如下:

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

这样使用:

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

em>对于尚未设置验证的用户:

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

内容验证 >您必须使用 zfcampus / zf-content-validation 模块,并按照文档进行配置。此模块允许您在 input_filter_spec 中配置输入过滤器和验证器,就像您通常在ZF2中进行表单验证一样。在这些 input-filter 配置数组里面你可以使用我上面引用的配置。

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.

该模块,一旦设置,您将能够使用 Apigility 中的这些验证类型。

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

这篇关于ZF2 apigility - 我们如何验证json数据中的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆