10000 [Form] Allowing plural message on extra data validation failure by popnikos · Pull Request #34984 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] Allowing plural message on extra data validation failure #34984

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 24, 2020
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
Allowing plural message on extra data validation failure
When using allow_extra_fields feature (and set to false) with an extra_fields_message, this message may now support pluralization regarding count of extra fields (extra data). e.g. "extra field found|extra fields found"
  • Loading branch information
popnikos authored and xabbuh committed Mar 24, 2020
commit 0b058fa0a05c475f8a530714a84ee24eebea7530
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ public function validate($form, Constraint $formConstraint)
$this->context->setConstraint($formConstraint);
$this->context->buildViolation($config->getOption('extra_fields_message', ''))
->setParameter('{{ extra_fields }}', '"'.implode('", "', array_keys($form->getExtraData())).'"')
->setPlural(\count($form->getExtraData()))
->setInvalidValue($form->getExtraData())
->setCode(Form::NO_SUCH_FIELD_ERROR)
->addViolation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ public function testDontWalkScalars()

public function testViolationIfExtraData()
{
$form = $this->getBuilder('parent', null, ['extra_fields_message' => 'Extra!'])
$form = $this->getBuilder('parent', null, ['extra_fields_message' => 'Extra!|Extras!'])
->setCompound(true)
->setDataMapper(new PropertyPathMapper())
->add($this->getBuilder('child'))
Expand All @@ -662,16 +662,17 @@ public function testViolationIfExtraData()

$this->validator->validate($form, new Form());

$this->buildViolation('Extra!')
$this->buildViolation('Extra!|Extras!')
->setParameter('{{ extra_fields }}', '"foo"')
->setInvalidValue(['foo' => 'bar'])
->setPlural(1)
->setCode(Form::NO_SUCH_FIELD_ERROR)
->assertRaised();
}

public function testViolationFormatIfMultipleExtraFields()
{
$form = $this->getBuilder('parent', null, ['extra_fields_message' => 'Extra!'])
$form = $this->getBuilder('parent', null, ['extra_fields_message' => 'Extra!|Extras!!'])
->setCompound(true)
->setDataMapper(new PropertyPathMapper())
->add($this->getBuilder('child'))
Expand All @@ -685,9 +686,10 @@ public function testViolationFormatIfMultipleExtraFields()

$this->validator->validate($form, new Form());

$this->buildViolation('Extra!')
$this->buildViolation('Extra!|Extras!!')
->setParameter('{{ extra_fields }}', '"foo", "baz", "quux"')
->setInvalidValue(['foo' => 'bar', 'baz' => 'qux', 'quux' => 'quuz'])
->setPlural(3)
->setCode(Form::NO_SUCH_FIELD_ERROR)
->assertRaised();
}
Expand Down
0