Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 阵列的Symfony2验证组_Php_Arrays_Validation_Symfony - Fatal编程技术网

Php 阵列的Symfony2验证组

Php 阵列的Symfony2验证组,php,arrays,validation,symfony,Php,Arrays,Validation,Symfony,由于某些条件,我想使用验证组验证数组,但验证组似乎不会影响数组 $params = [ 'type' => 'a', 'province' => 'b', 'district' => 'c' ]; $constraints = new Collection([ 'type' => [new NotBlank()], 'province' => [new NotBlank(['groups' => ['selectio

由于某些条件,我想使用验证组验证数组,但验证组似乎不会影响数组

$params = [
    'type' => 'a',
    'province' => 'b',
    'district' => 'c'
];

$constraints = new Collection([
    'type' => [new NotBlank()],

    'province' => [new NotBlank(['groups' => ['selection']])],
    'district' => [new NotBlank(['groups' => ['selection']])],

    'distance' => [new NotBlank(['groups' => ['location']])],
    'lat' => [new NotBlank(['groups' => ['location']])],
    'lon' => [new NotBlank(['groups' => ['location']])],
]);

$errors = $this->container->get('validator')->validate($params, $constraints, ['selection']);
验证错误:

Array[distance]:
This field is missing. (code 1)
Array[lat]:
This field is missing. (code 1)
Array[lon]:
This field is missing. (code 1)

谢谢你的帮助

您需要像这样使用
'allowMissingFields'=>true,

$constraints = new Collection(
'allowMissingFields' => true,
'fields' => [
    'type' => [new NotBlank()],

    'province' => [new NotBlank(['groups' => ['selection']])],
    'district' => [new NotBlank(['groups' => ['selection']])],

    'distance' => [new NotBlank(['groups' => ['location']])],
    'lat' => [new NotBlank(['groups' => ['location']])],
    'lon' => [new NotBlank(['groups' => ['location']])],
]);

CollectionValidator正在验证约束NotBlank之前检查字段是否存在