Validate multiple groups sequentially in Symfony

Code
<?php

use Symfony\Component\Validator\ValidatorBuilder;
use 
Symfony\Component\Validator\Constraints as C;

$constraints = [
    new 
C\Type(['groups' => ['step1'], 'type' => ArrayObject::class]),
    new 
C\Count(['groups' => ['step2'], 'min' => 1]),
];

$groupSequence = new C\GroupSequence(['step1''step2']);

$validator = (new ValidatorBuilder())
    ->
enableAnnotationMapping()
    ->
getValidator();

dump(
    
// if group "step1" fails, group "step2" won't be checked
    
$validator->validate('foo'$constraints$groupSequence),
    
$validator->validate(new ArrayObject(), $constraints$groupSequence)
);
Result
ConstraintViolationList {#396
-violations: array:1 [
0 => ConstraintViolation {#400
-message: "This value should be of type ArrayObject."
-messageTemplate: "This value should be of type {{ type }}."
-parameters: array:2 [
"{{ value }}" => ""foo""
"{{ type }}" => "ArrayObject"
]
-plural: null
-root: "foo"
-propertyPath: ""
-invalidValue: "foo"
-constraint: Type {#380
+message: "This value should be of type {{ type }}."
+type: "ArrayObject"
+payload: null
+"groups": array:1 [
0 => "step1"
]
}
-code: "ba785a8c-82cb-4283-967c-3cf342181b40"
-cause: null
}
]
}

ConstraintViolationList {#401
-violations: array:1 [
0 => ConstraintViolation {#404
-message: "This collection should contain 1 element or more."
-messageTemplate: "This collection should contain {{ limit }} element or more.|This collection should contain {{ limit }} elements or more."
-parameters: array:2 [
"{{ count }}" => 0
"{{ limit }}" => 1
]
-plural: 1
-root: ArrayObject {#387
flag::STD_PROP_LIST: false
flag::ARRAY_AS_PROPS: false
iteratorClass: "ArrayIterator"
storage: []
}
-propertyPath: ""
-invalidValue: ArrayObject {#387}
-constraint: Count {#379
+minMessage: "This collection should contain {{ limit }} element or more.|This collection should contain {{ limit }} elements or more."
+maxMessage: "This collection should contain {{ limit }} element or less.|This collection should contain {{ limit }} elements or less."
+exactMessage: "This collection should contain exactly {{ limit }} element.|This collection should contain exactly {{ limit }} elements."
+min: 1
+max: null
+payload: null
+"groups": array:1 [
0 => "step2"
]
}
-code: "bef8e338-6ae5-4caf-b8e2-50e7b0579e69"
-cause: null
}
]
}
Used Versions
PHP 7.2, Zend 3.0, Symfony 4.0, Laravel 5.6, PHPUnit 6.5, Doctrine 2.8