DbNoRecordExist Validator

How to use exclude in DbNoRecordExist Validator ? I want to give like this; exclude => [field => “id” , value=> ‘value of id’]. How to get value of ‘id’ without giving constant value ?

Hello! Did you find a solution?

Hey,

just put the exclude field and value pair in the validator options as you can see in the offical documentation.

$validator = new Laminas\Validator\Db\NoRecordExists([
    'table' => 'users',
    'field' => 'username',
    'exclude' => [
        'field' => 'id',
        'value' => $user_id,
    ],
]);

Think of a factory, that checks the request query parameters for the given user id. When writing your own input filter classes, you can use this validator factory.

$request = $container->get('Application')->getMvcEvent()->getRequest();
$validators = $container->get(ValidatorPluginManager::class);

$user_id = $request->getQuery('id', null);

$validator = $validators->build(
    'Laminas\ApiTools\ContentValidation\Validator\DbNoRecordExists',
    [
        'exclude' => [
            'field' => 'id',
            'value' => $user_id,
        ],
    ]
);
1 Like