[英]How to set up input validation in ZF2/Apigility, if rules are field-dependent?
There is a class Item
. 有一个类Item
。 It has an attribute type
, that can be a
, b
, or c
. 它具有属性type
,可以是a
, b
或c
。 For all the types there is a common minimal set of attributes / input fields: type
and other ones. 对于所有类型,都有一个通用的最小属性集/输入字段: type
和其他。 Every type has some further attributes: 每种类型都有一些其他属性:
default set of the common fields
type
...
additional fields in case of type=a
foo
additional fields in case of type=b
bar
baz
additional fields in case of type=c
bar
baz
buz
Furthermore the validation rules for bar
and bar
are slightly different for the cases type=b
and type=c
. 此外,对于type=b
和type=c
情况, bar
和bar
的验证规则略有不同。
How to set up the validation in a ZF2/Apigilty application depending on the value of a field (or multiple fields)? 如何根据一个字段(或多个字段)的值在ZF2 / Apigilty应用程序中设置验证? For this concrete case: How to set up the validation depending on the type
? 对于这种具体情况:如何根据type
设置验证?
UPDATE UPDATE
The attribute type
is an in dependent one. 属性type
是in依赖的。 That means -- it should not become invalid, if the set of the additional fields ( foo
, bar
etc.) doesn't match to it. 这意味着-它不应该成为无效,如果设定的附加字段(中foo
, bar
等)不匹配它。 (It's required
and gets validated agains an array of the allowed values, that's it.) (这是required
并且再次验证了允许值的数组 ,就是这样。)
So, it should work in the opposite direction: 因此,它应该朝相反的方向工作:
IF (type == 'a') {
proper "required" and validation rules the additional fields
} ELSEIF (type == 'b') {
proper "required" and validation rules the additional fields
} ELSEIF (type == 'c') {
proper "required" and validation rules the additional fields
}
You can write a ValidTypeInputFilter
with your custom logic and attach a config that looks something similar like this: 您可以使用自定义逻辑编写ValidTypeInputFilter
并附加一个类似于以下内容的配置:
array(
'type' => array(
'name' => 'type',
'required' => true,
'validators' => array(
array(
'name' => 'CustomTypeValidator'
)
)
),
'qwer' => array(
'name' => 'qwer',
'required' => true,
'filters' => array(
)
'validators' => array(
)
),
'asdf' => array(
'name' => 'asdf',
'required' => true,
'filters' => array(
)
'validators' => array(
)
),
'yxcv' => array(
'name' => 'yxcv',
'required' => true,
'filters' => array(
)
'validators' => array(
)
)
)
And your custom validator class: 和您的自定义验证器类:
<?php
namespace Application\Validator;
class CustomTypeValidator extends AbstractValidator{
const INVALID_TYPE = 'invalid_type';
const NOT_ARRAY = 'not_an_array';
protected $messageTemplates = array(
self::NOT_ARRAY => "Type must be an array",
self::INVALID_TYPE => "Type must be a, b or c",
);
public function isValid($value)
{
$this->setValue($value);
if (!is_array($value)) {
$this->error(self::NOT_ARRAY);
return false;
}
if(key($value) === 'a'){
//... validate a
}
if(key($value) === 'b'){
//... validate b
}
if(key($value) === 'c'){
//... validate c
}
$this->error(self::INVALID_TYPE);
return false;
}
}
For validating a
, b
and c
you can also write validator classes and instantiate and use them inside your custom type validator: 为了验证a
, b
和c
您还可以编写验证器类并实例化并在自定义类型验证器中使用它们:
if(key($value) === 'a'){
$validator = new TypeAValidator():
$valid = $validator->isValid($value['a']);
return $valid;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.