1

我使用最新版本的 Laminas。我有一个通过添加到表单的init()字段集,并将其设置为基本字段集。如果您在字段集中定义getInputFilterSpecification(),在更新 Laminas 框架之前(我必须承认,我有几个月没有更新),我可以通过在字段集内部调用来检索字段集输入的过滤$this->get('someName')->getValue()getInputFilterSpecification()。这非常方便,因为如果您有一个通过工厂初始化的复杂验证器,并且您想将其他输入的值传递给该验证器以实现复杂逻辑,您只需将所需的输入值分配给'options'验证器,如下所示:

'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(),
                ],
        ],
        ...
    ],
],

并且已经获得了输入的过滤值parameter1parameter2因此无需在验证器中重新处理值。现在那里只提供原始的、未过滤的值$this->get('otherName2')->getValue()

有没有办法为验证器工厂提供字段集输入的过滤值?还是我这样做完全错误?

4

1 回答 1

1

正确的做法是使用第二个$context参数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
    }
}

相同的验证器正在使用它,例如:https ://github.com/laminas/laminas-validator/blob/2.16.x/src/Identical.php#L165

于 2021-12-30T13:17:58.700 回答