zendframework / zend-inputfilter

InputFilter component from Zend Framework
BSD 3-Clause "New" or "Revised" License
64 stars 50 forks source link

How to ... in InputFilter #106

Closed snapshotpl closed 8 years ago

snapshotpl commented 8 years ago

I use InputFilter in Apigility validation POST data. I want to make optional user object in json. However if it's appear then some fields should be required.

{
  "number": "1234",
  "user": {
    "name": "John",
    "company": {
        "name": "Company name",
        "vat_id": "00011114444",
        "address": {
          "locality": "Paris",
          "postal_code": "333",
          "country": "FR"
        }
    }
  }
}

My InputFilter config:

return [
    'input_filter_specs' => [
        'OrdersApi\V1\Rest\ShippingOrders\Validator' => [
            'number' => [
                'required' => true,
            ],
            'user' => [
                'type' => Zend\InputFilter\InputFilter::class,
                'company' => [
                    'type' => Zend\InputFilter\InputFilter::class,
                    'name' => [],
                    'vat_id' => [],
                    'address' => [
                        'type' => Zend\InputFilter\InputFilter::class,
                        'locality' => [
                           'required' => false,
                        ],
                        'postal_code' => [],
                        'country' => [],
                    ],
                ],
            ],
        ],
    ],
];

In this case I need to send all fields (only locality is not required), but I need to be able to not send whole user object.

Any advice?

Maks3w commented 8 years ago

This issue tracker is only for bugs and feature requests. Please post your question in a Q&A site like Stackoverflow.

Hint: Play with this snippet

$inputSpecification = [
  'allow_empty'       => true, // keep this as true always
  'continue_if_empty' => true,  // keep this as true always
  'validators' => [
    [
      'break_chain_on_failure' => true,
      'name'                   => 'Zend\\Validator\\NotEmpty',
    ],
  ],
];