zend-frameworkzend-framework3laminas

Laminas, problem with passing filtered fieldset inputs values to validator factory


I use Laminas of most recent version. I have a fieldset that is added to a form via init() and it is set as a base fieldset. If you define getInputFilterSpecification() in the fieldset, before the update of the Laminas framework (I must admit, I didn't update for a couple of months), I could retrieve the filtered values of the fieldset inputs just by calling $this->get('someName')->getValue() inside the fieldset's getInputFilterSpecification(). This was very convenient, because if you have a complex validator with initialization through factory and you wanted to pass the values of other inputs to this validator for complex logic, you could just assign the needed input values to 'options' in the validator, like this:

'someInputName' => [
    'required' => false,
    'filters' => [
        ['name' => StripTags::class],
        ['name' => StringTrim::class],
        ...
    ],
    'validators' => [
        ['name' => NotEmpty::class],
        [
            'name' => AComplexValidator::class,
                'options' => [
                    'parameter1' =>
                        $this->get('otherName1')->getValue(),
                    'parameter2' =>
                        $this->get('otherName2')->getValue(),
                ],
        ],
        ...
    ],
],

and parameter1 and parameter2 already got the filtered values of the inputs, so there is no need to reprocess values in the validator. Now there are only raw, unfiltered values provided there $this->get('otherName2')->getValue().

Is there a way to supply the validator factory with the filtered values of the fieldset inputs? Or do I do this completely wrong?


Solution

  • The correct approach is to use the second parameter $context in isValid():

    <?php
    class AComplexValidator extends AbstractValidator
    {
        public function isValid(mixed $value, ?iterable $context = null): bool {
            // $context will contain all other field values of the Fieldset
        }
    }
    

    The Identical validator is useing this for example: https://github.com/laminas/laminas-validator/blob/2.16.x/src/Identical.php#L165