Custom validators with params in Laminas API Tools

706 Views Asked by At

How can I add custom validators with parameters in API Tools GUI?

I wrote a custom validator + factory.

I have some setters+getters there.

How can I configure this validator the way it would:

  • show up in the list of available validators in the GUI (I'm adding a factory to validators.factories key in the module.config.php
  • allow the user to choose parameters (eg. maxlen, minlen etc.). Now it shows only one option available: breakchainonfailure

Similar to the StringLength validator:

Options

My factory:

<?php

namespace My\Validator\DoctrineConnected\Factory;

use Interop\Container\ContainerInterface;
use Interop\Container\Exception\ContainerException;
use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
use Laminas\ServiceManager\Exception\ServiceNotFoundException;
use Laminas\ServiceManager\Factory\AbstractFactoryInterface;

class ExistsValidatorFactory implements AbstractFactoryInterface {
    /**
     * Create an object
     *
     * @param  ContainerInterface $container
     * @param  string $requestedName
     * @param  null|array $options
     * @return object
     * @throws ServiceNotFoundException if unable to resolve the service.
     * @throws ServiceNotCreatedException if an exception is raised when
     *     creating a service.
     * @throws ContainerException if any other error occurs
     */
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null) {
        $entityManger = $container->get('doctrine.entitymanger.orm_default');
        return new $requestedName($entityManger);
    }

    public function canCreate(ContainerInterface $container, $requestedName)
    {
        return class_exists($requestedName);
    }
}

1

There are 1 best solutions below

0
Marcel On

You have to define the validator metadata in your module.config.php of your module.

return [
    ...
    'validators' => [
        'factories' => [
            Validator\YourValidator::class => Validator\Factory\YourValidatorFactory::class,
        ],
    ],
    'validator_metadata' => [
        Validator\YourValidator::class => [
            'entity_class' => 'string',
            'fields' => 'string'
        ],
    ],
    ...
];

For the defined options in the validator metadata you can set default values in the validator factory. Please avoid using the Interop\Container package and use the Psr\Container package.

<?php
declare(strict_types=1);
namespace Application\Validator\Factory;

use Application\Validator\YourValidator;
use DoctrineORMModule\Options\EntityManager;
use Laminas\Stdlib\ArrayUtils;
use Psr\Container\ContainerInterface;

class YourValidatorFactory
{
    public function __invoke(
        ContainerInterface $container, 
        string $requestedName, 
        array $options = []
    ): AbstractValidator
    {
        if (isset($options['entity_class'])) {
            $repository = $container
                ->get(EntityManager::class)
                ->getRepository($options['entity_class']);
        }

        $options = ArrayUtils::merge($options, [ 
            'object_repository' => $repository,
        ]);
    }

    return new YourValidator($options);
}

The options you 've defined in the validator config will be passed as $options parameter to the validator factory. The breakchainonfailure is a default option for validators in the laminas framework.