[Validator] Fixed default group for nested composite constraints
This commit is contained in:
parent
5da141b8d0
commit
117ee34698
@ -88,7 +88,8 @@ abstract class Composite extends Constraint
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->groups = array_keys($mergedGroups);
|
// prevent empty composite constraint to have empty groups
|
||||||
|
$this->groups = array_keys($mergedGroups) ?: [self::DEFAULT_GROUP];
|
||||||
$this->$compositeOption = $nestedConstraints;
|
$this->$compositeOption = $nestedConstraints;
|
||||||
|
|
||||||
return;
|
return;
|
||||||
|
@ -100,4 +100,16 @@ class CollectionTest extends TestCase
|
|||||||
|
|
||||||
$this->assertEquals($collection1, $collection2);
|
$this->assertEquals($collection1, $collection2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testConstraintHasDefaultGroupWithOptionalValues()
|
||||||
|
{
|
||||||
|
$constraint = new Collection([
|
||||||
|
'foo' => new Required(),
|
||||||
|
'bar' => new Optional(),
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->assertEquals(['Default'], $constraint->groups);
|
||||||
|
$this->assertEquals(['Default'], $constraint->fields['foo']->groups);
|
||||||
|
$this->assertEquals(['Default'], $constraint->fields['bar']->groups);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -143,6 +143,29 @@ abstract class CollectionValidatorTest extends ConstraintValidatorTestCase
|
|||||||
->assertRaised();
|
->assertRaised();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testExtraFieldsDisallowedWithOptionalValues()
|
||||||
|
{
|
||||||
|
$constraint = new Optional();
|
||||||
|
|
||||||
|
$data = $this->prepareTestData([
|
||||||
|
'baz' => 6,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->validator->validate($data, new Collection([
|
||||||
|
'fields' => [
|
||||||
|
'foo' => $constraint,
|
||||||
|
],
|
||||||
|
'extraFieldsMessage' => 'myMessage',
|
||||||
|
]));
|
||||||
|
|
||||||
|
$this->buildViolation('myMessage')
|
||||||
|
->setParameter('{{ field }}', '"baz"')
|
||||||
|
->atPath('property.path[baz]')
|
||||||
|
->setInvalidValue(6)
|
||||||
|
->setCode(Collection::NO_SUCH_FIELD_ERROR)
|
||||||
|
->assertRaised();
|
||||||
|
}
|
||||||
|
|
||||||
// bug fix
|
// bug fix
|
||||||
public function testNullNotConsideredExtraField()
|
public function testNullNotConsideredExtraField()
|
||||||
{
|
{
|
||||||
|
@ -19,7 +19,7 @@ use Symfony\Component\Validator\Constraints\Valid;
|
|||||||
|
|
||||||
class ConcreteComposite extends Composite
|
class ConcreteComposite extends Composite
|
||||||
{
|
{
|
||||||
public $constraints;
|
public $constraints = [];
|
||||||
|
|
||||||
protected function getCompositeOption()
|
protected function getCompositeOption()
|
||||||
{
|
{
|
||||||
@ -37,6 +37,30 @@ class ConcreteComposite extends Composite
|
|||||||
*/
|
*/
|
||||||
class CompositeTest extends TestCase
|
class CompositeTest extends TestCase
|
||||||
{
|
{
|
||||||
|
public function testConstraintHasDefaultGroup()
|
||||||
|
{
|
||||||
|
$constraint = new ConcreteComposite([
|
||||||
|
new NotNull(),
|
||||||
|
new NotBlank(),
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->assertEquals(['Default'], $constraint->groups);
|
||||||
|
$this->assertEquals(['Default'], $constraint->constraints[0]->groups);
|
||||||
|
$this->assertEquals(['Default'], $constraint->constraints[1]->groups);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testNestedCompositeConstraintHasDefaultGroup()
|
||||||
|
{
|
||||||
|
$constraint = new ConcreteComposite([
|
||||||
|
new ConcreteComposite(),
|
||||||
|
new ConcreteComposite(),
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->assertEquals(['Default'], $constraint->groups);
|
||||||
|
$this->assertEquals(['Default'], $constraint->constraints[0]->groups);
|
||||||
|
$this->assertEquals(['Default'], $constraint->constraints[1]->groups);
|
||||||
|
}
|
||||||
|
|
||||||
public function testMergeNestedGroupsIfNoExplicitParentGroup()
|
public function testMergeNestedGroupsIfNoExplicitParentGroup()
|
||||||
{
|
{
|
||||||
$constraint = new ConcreteComposite([
|
$constraint = new ConcreteComposite([
|
||||||
|
Reference in New Issue
Block a user