我目前正在开发的 ZF2 应用程序是 Apigility 驱动的,但我不得不将一些InputFilter
设置从module.config.php
单独的InputFilter
文件/类中移动。
现在我(除其他外)有字段CompanyId
和UserId
. 必须设置其中一个(一个且只有一个)。我已经用Callback
验证器尝试过这个,见下文。但它不起作用:如果两个字段都缺失,则不会执行验证。
如何解决这个问题?
class AddressPostRequestInput implements ArraySerializableInterface {
...
public function __construct() {
$this->dataSet = [
'CompanyId' => null,
'UserId' => null,
'Type' => null,
];
}
...
public function getInputFilter() {
if (!$this->inputFilter) {
$inputFilter = new InputFilter();
$factory = new InputFactory();
...
$inputFilter->add($factory->createInput(array(
...
$inputFilter->add($factory->createInput(array(
'name' => 'CompanyId',
'required' => false,
'filters' => array(
...
),
'validators' => array(
array(
'name' => 'Callback',
'options' => array(
'callback' => function ($value, $context = array()) {
$isValid =
(isset($context['UserId']) && !isset($context['CompanyId'])) ||
(!isset($context['UserId']) && isset($context['CompanyId']))
;
return $isValid;
},
),
),
$inputFilter->add($factory->createInput(array(
'name' => 'UserId',
'required' => false,
'filters' => array(
...
),
'validators' => array(
array(
'name' => 'Callback',
'options' => array(
'callback' => function ($value, $context = array()) {
$isValid =
(isset($context['UserId']) && !isset($context['CompanyId'])) ||
(!isset($context['UserId']) && isset($context['CompanyId']))
;
return $isValid;
},
),
),
)));
$this->inputFilter = $inputFilter;
}
...
}