ZF3-回调验证器将错误消息附加到错误的输入

时间:2019-07-12 15:33:03

标签: php zend-framework zend-framework3

我有一个表单,其中应根据选择字段中的选项要求单选按钮字段。如果选择了单选按钮选项“是”,则还需要一个文本字段。 我正在为两个字段使用回调验证器来检查此依赖性,但是问题是错误消息被附加到错误的字段。例如,如果我从选择中选择一个选项,而未选中单选按钮,则验证程序会起作用,但是错误消息将显示在选择输入中,而不是显示在单选按钮中。

根据这篇文章中“ rkeet”的回答,我为此字段编写了两个验证器:

https://github.com/zendframework/zend-inputfilter/issues/146

$inputFilter->add([
            'name' => 'regimenPreferencia',
            'required' => true,
            'validators' => [
                [
                    'name' => NotEmpty::class,
                    'options' => [
                        'message' => [
                            NotEmpty::IS_EMPTY => 'Ingrese el régimen de preferencia para este artículo'
                        ]
                    ],
                ],
                [
                    'name' => Callback::class,
                    'options' => [
                        'callback' => function($value, $context) {

                            var_dump($value, $context);
                            if($value === 'agricultura_familiar' && empty($context['esCompraCentralizada'])) {
                                $validatorChain = $this->getInputFilter()->getInputs()['esCompraCentralizada']->getValidatorChain();
                                $validatorChain->attach(new NotEmpty(['type' => NotEmpty::NULL]));
                                $this->getInputFilter()->getInputs()['esCompraCentralizada']->setValidatorChain($validatorChain);

                                return false;
                            }

                            return true;
                        },
                        'messages' => [
                            Callback::INVALID_VALUE => 'Indique si esta compra es centralizada'
                        ]
                    ],
                ]
            ],
            'allow_empty' => false,
            'continue_if_empty' => false,
        ]);

        $inputFilter->add([
            'name' => 'esCompraCentralizada',
            'required' => false,
            'allow_empty' => true,
            'validators' => [
                [
                    'name' => Callback::class,
                    'options' => [
                        'callback' => function($value, $context) {

                            if(strlen($value) > 0 && empty($context['porcAdjudicacionReservaMercado'])) {
                                $validatorChain = $this->getInputFilter()->getInputs()['porcAdjudicacionReservaMercado']->getValidatorChain();
                                $validatorChain->attach(new NotEmpty(['type' => NotEmpty::NULL]));
                                $this->getInputFilter()->getInputs()['porcAdjudicacionReservaMercado']->setValidatorChain($validatorChain);

                                return false;
                            }

                            return true;
                        },
                        'messages' => [
                            'callbackValue' => 'Ingrese el porcentaje de adjudicación de reserva de mercado'
                        ]
                    ],
                ]
            ],

            'continue_if_empty' => true
        ]);

        $inputFilter->add([
            'name' => 'porcAdjudicacionReservaMercado',
            'allow_empty' => true,
            'filters' => [
                ['name' => ToInt::class]
            ],
            'validators' => [

            ],
        ]);

1 个答案:

答案 0 :(得分:0)

好吧,我按预期工作了。 我在esCompraCentralizada中添加了一个回调验证器,以检查其是否为空,并将其余验证器添加到porcAdjudicacionReservaMercado字段中。

这就是验证者最终的身份:

$inputFilter->add([
        'name' => 'esCompraCentralizada',
        'required' => false,
        'allow_empty' => true,
        'validators' => [
            [
                'name' => Callback::class,
                'brake_chain_on_failure' => true,
                'options' => [
                    'callback' => function($value, $context) {
                        if(!isset($context['esCompraCentralizada']) && $context['regimenPreferencia'] === 'agricultura_familiar') {
                            $validatorChain = $this->getInputFilter()->getInputs()['esCompraCentralizada']->getValidatorChain();
                            $validatorChain->attach(new NotEmpty(['type' => NotEmpty::NULL]));
                            $this->getInputFilter()->getInputs()['esCompraCentralizada']->setValidatorChain($validatorChain);

                            return false;
                        }

                        return true;
                    },
                    'messages' => [
                        Callback::INVALID_VALUE => 'Indique si esta compra es centralizada'
                    ]
                ],
            ]
        ]
    ]);

    $inputFilter->add([
        'name' => 'porcAdjudicacionReservaMercado',
        'required' => false,
        'allow_empty' => true,
        'filters' => [
            ['name' => ToInt::class]
        ],
        'validators' => [
            [
                'name' => Callback::class,
                'break_chain_on_failure' => true,
                'options' => [
                    'callback' => function($value, $context) {
                        if($context['porcAdjudicacionReservaMercado'] === '' && $context['esCompraCentralizada'] === '1' && $context['regimenPreferencia'] === 'agricultura_familiar') {
                            $validatorChain = $this->getInputFilter()->getInputs()['porcAdjudicacionReservaMercado']->getValidatorChain();
                            $validatorChain->attach(new NotEmpty(['type' => NotEmpty::NULL]));
                            $this->getInputFilter()->getInputs()['porcAdjudicacionReservaMercado']->setValidatorChain($validatorChain);

                            return false;
                        }

                        return true;
                    },
                    'messages' => [
                        Callback::INVALID_VALUE => 'Ingrese el porcentaje de adjudicación de reserva de mercado'
                    ]
                ],
            ],
            [
                'name' => Callback::class,
                'break_chain_on_failure' => true,
                'options' => [
                    'callback' => function($value, $context) {
                        if (!is_numeric($context['porcAdjudicacionReservaMercado']) && $context['regimenPreferencia'] === 'agricultura_familiar' && $context['esCompraCentralizada'] === '1') {
                            $validatorChain = $this->getInputFilter()->getInputs()['porcAdjudicacionReservaMercado']->getValidatorChain();
                            $validatorChain->attach(new LessThan());
                            $this->getInputFilter()->getInputs()['porcAdjudicacionReservaMercado']->setValidatorChain($validatorChain);

                            return false;
                        }

                        return true;
                    },
                    'message' => [
                        Callback::INVALID_VALUE => 'El porcentaje de adjudicación de reserva de mercado debe ser un valor numérico'
                    ]
                ],
            ],
            [
                'name' => Callback::class,
                'break_chain_on_failure' => true,
                'options' => [
                    'callback' => function($value, $context) {
                        if ($value > 100 && $context['regimenPreferencia'] === 'agricultura_familiar' && $context['esCompraCentralizada'] === '1') {
                            $validatorChain = $this->getInputFilter()->getInputs()['porcAdjudicacionReservaMercado']->getValidatorChain();
                            $validatorChain->attach(new LessThan());
                            $this->getInputFilter()->getInputs()['porcAdjudicacionReservaMercado']->setValidatorChain($validatorChain);

                            return false;
                        }

                        return true;
                    },
                    'message' => [
                        Callback::INVALID_VALUE => 'El porcentaje de adjudicación de reserva de mercado debe ser menor o igual a 100'
                    ]
                ],
            ],
            [
                'name' => Callback::class,
                'break_chain_on_failure' => true,
                'options' => [
                    'callback' => function($value, $context) {
                        if ($value < 30 && $context['regimenPreferencia'] === 'agricultura_familiar' && $context['esCompraCentralizada'] === '1') {
                            $validatorChain = $this->getInputFilter()->getInputs()['porcAdjudicacionReservaMercado']->getValidatorChain();
                            $validatorChain->attach(new GreaterThan());
                            $this->getInputFilter()->getInputs()['porcAdjudicacionReservaMercado']->setValidatorChain($validatorChain);

                            return false;
                        }

                        return true;
                    },
                    'message' => [
                        Callback::INVALID_VALUE => 'El porcentaje de precio de materiales nacionales debe ser mayor o igual a 30'
                    ]
                ],
            ],
        ],
    ]);

我在第二个字段中添加了四个验证器以进行检查:

  • 空字段
  • 字段值不是数字
  • 字段值小于30(功能要求)
  • 字段值大于100(功能要求)

如果“ regimen_preferencia”为“ agricultura_familiar”且“ esCompraCentralizada”为true,则所有这一切都适用。

非常感谢您提供的所有帮助!