ephbaum
ephbaum

Reputation: 362

How do I get Apigility API to use my custom validator?

I have written a custom validator that extends Zend\Validator\AbstractValidator that is meant to check a field in my Apigility based REST API. I've been digging through documentation and examples and I have yet to see an example of this being done.

Within my module.config.php I have added it to the field under the validator within input_filter_specs. Like so:

...

'input_filter_specs' => [
    'Application\\V1\\Rest\\Class\\Validator' => [
        [
            'type' => 'Float',
            'required' => true,
            'validators' => [
                'name' => 'Zend\\I18n\\Validator\\IsFloat',
                'options' => [],
            ],
            'filters' => [],
            'name' => 'aFloatParameter',
            'description' => 'Float based parameter',
        ],
        [
            'type' => 'String',
            'required' => false,
            'validators' => [
                'name' => 'Application\\Validator\\CustomValidator',
                'options' => [],
            ],
            'filters' => [],
            'name' => 'parameterToValidate',
            'description' => 'This is a parameter to be validator',
        ],
],

...

When I make the request the native Zend validators all work as expected but my custom validator class does not.

There are no errors, it just simply does not fire.

Can anyone help me figure out what I am missing? Do I need to register the validator somewhere else as well?

TIA

Upvotes: 0

Views: 309

Answers (1)

You should mention your validator in validators key of config under invokable or factories key. Also Zend/Validator module have to be in your modules config. So modules config like;

return [
    'modules' => [
       ....
        'Zend\Validator',
        ....
    ]
];

and validators config;

return [
    'validators' => [
       'invokables' => [
           'Application\\Validator\\CustomValidator',
        ]
    ]
];

In this way, ValidatorManager will know what it is and how to call it.

Upvotes: 1

Related Questions