8000 [Validator] Choices constraint improvement by nikophil · Pull Request #29658 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
add new choices wildcard in message
  • Loading branch information
nikophil committed Dec 27, 2018
commit 71dfa35a215a01b39bca646b2df473f62f5beb29
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public function validate($value, Constraint $constraint)
if (!\in_array($_value, $choices, true)) {
$this->context->buildViolation($constraint->multipleMessage)
->setParameter('{{ value }}', $this->formatValue($_value))
->setParameter('{{ choices }}', $this->formatValues($choices))
->setCode(Choice::NO_SUCH_CHOICE_ERROR)
->setInvalidValue($_value)
->addViolation();
Expand Down Expand Up @@ -100,6 +101,7 @@ public function validate($value, Constraint $constraint)
} elseif (!\in_array($value, $choices, true)) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setParameter('{{ choices }}', $this->formatValues($choices))
->setCode(Choice::NO_SUCH_CHOICE_ERROR)
->addViolation();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ public function testInvalidChoice()

$this->buildViolation('myMessage')
->setParameter('{{ value }}', '"baz"')
->setParameter('{{ choices }}', '"foo", "bar"')
->setCode(Choice::NO_SUCH_CHOICE_ERROR)
->assertRaised();
}
Expand All @@ -186,6 +187,7 @@ public function testInvalidChoiceEmptyChoices()

$this->buildViolation('myMessage')
->setParameter('{{ value }}', '"baz"')
->setParameter('{{ choices }}', '')
->setCode(Choice::NO_SUCH_CHOICE_ERROR)
->assertRaised();
}
Expand All @@ -202,6 +204,7 @@ public function testInvalidChoiceMultiple()

$this->buildViolation('myMessage')
->setParameter('{{ value }}', '"baz"')
->setParameter('{{ choices }}', '"foo", "bar"')
->setInvalidValue('baz')
->setCode(Choice::NO_SUCH_CHOICE_ERROR)
->assertRaised();
Expand Down Expand Up @@ -275,6 +278,7 @@ public function testStrictDisallowsDifferentType()

$this->buildViolation('myMessage')
->setParameter('{{ value }}', '"2"')
->setParameter('{{ choices }}', '1, 2')
->setCode(Choice::NO_SUCH_CHOICE_ERROR)
->assertRaised();
}
Expand All @@ -291,6 +295,7 @@ public function testStrictWithMultipleChoices()

$this->buildViolation('myMessage')
->setParameter('{{ value }}', '"3"')
->setParameter('{{ choices }}', '1, 2, 3')
->setInvalidValue('3')
->setCode(Choice::NO_SUCH_CHOICE_ERROR)
->assertRaised();
Expand Down
0