I want to validate an array using validation group because of some conditions but validation group doesn't seem affect array?
$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']);
Validation errors:
Array[distance]:
This field is missing. (code 1)
Array[lat]:
This field is missing. (code 1)
Array[lon]:
This field is missing. (code 1)
Thank for your help!
You need to use 'allowMissingFields' => true,
like this :
$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']])],
]);
https://symfony.com/doc/2.7/reference/constraints/Collection.html
The CollectionValidator is checking if the field exists before the constraint NotBlank is validated.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.