ZF3 - 回调验证器将错误消息附加到错误输入
ZF3 - Callback validator attaches error message to wrong input
我有一个表单,根据 select 字段中的选项,应该需要一个单选按钮字段。如果单选按钮选项 "yes" 被 selected,那么还有一个文本字段也应该是必需的。
我正在为这两个字段使用回调验证器来检查此依赖关系,但问题是错误消息被附加到错误的字段。例如,如果我 select 来自 select 的选项并且未选中单选按钮,则验证器工作,但错误消息显示在 select 输入中而不是单选按钮中。
我根据 "rkeet" 对此 post 的回答为该字段编写了两个验证器:
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' => [
],
]);
好的,我让它按预期工作了。
我向 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' 为真,则所有这些都适用。
非常感谢你的帮助!
我有一个表单,根据 select 字段中的选项,应该需要一个单选按钮字段。如果单选按钮选项 "yes" 被 selected,那么还有一个文本字段也应该是必需的。 我正在为这两个字段使用回调验证器来检查此依赖关系,但问题是错误消息被附加到错误的字段。例如,如果我 select 来自 select 的选项并且未选中单选按钮,则验证器工作,但错误消息显示在 select 输入中而不是单选按钮中。
我根据 "rkeet" 对此 post 的回答为该字段编写了两个验证器:
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' => [
],
]);
好的,我让它按预期工作了。 我向 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' 为真,则所有这些都适用。
非常感谢你的帮助!