1

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?

Dima Dz
  • 512
  • 1
  • 5
  • 17

1 Answers1

1

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

crash
  • 603
  • 4
  • 11
  • Man, this is awesome. Only trouble is that these input values are unfiltered (before the application of `StringTrim`, etc.). How can I get the filtered values of the form inputs into the validator? – Dima Dz Dec 31 '21 at 23:16
  • 1
    You could pass the InputFilter to the validator and then filter/validate that field inside of your validator. You just need to find the correct `Laminas\InputFilter\InputInterface` instance. – crash Jan 02 '22 at 14:09
  • in my view laminas could have developed a more 'in-system' approach to doing this, but in the meanwhile, that's a good workaround ;-) may i please ask you to update your answer? and i'll accept it. – Dima Dz Jan 02 '22 at 14:44
  • Honestly it's not a workaround, it's really how you should do it. I'm sorry but validating/filtering the values within the validator is out of scope of this question and requires A LOT of code. – crash Feb 10 '22 at 09:41
  • 1
    I understand, thanks for your input anyways – Dima Dz Apr 01 '22 at 17:46