New in v3 of laminas-validator: Conditional Validator

Version 3 of laminas-validator brings many changes in terms of code, but also new validators. Here is one of them: Laminas\Validator\Conditional

Laminas\Validator\Conditional allows you to validate a value conditionally depending on the outcome of a user-defined rule.

Example

$validator = new Conditional($chainFactory, [
    'rule' => static function (array $context): bool {
        return (bool) ($context['subscribe'] ?? null) === true;
    },
    'validators' => [
        ['name' => EmailAddress::class],
    ],
]);

$postPayload = [
    'subscribe' => '1',
    'email' => 'kermit@example.com',
];

$validator->isValid($postPayload['email'], $postPayload); // true