Skip to content

Commit

Permalink
feature #34984 [Form] Allowing plural message on extra data validatio…
Browse files Browse the repository at this point in the history
…n failure (popnikos)

This PR was merged into the 5.1-dev branch.

Discussion
----------

[Form] Allowing plural message on extra data validation failure

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| License       | MIT
| Doc PR        | -

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"

Commits
-------

0b058fa Allowing plural message on extra data validation failure
  • Loading branch information
fabpot committed Mar 24, 2020
2 parents f18294b + 0b058fa commit 8e47e9f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
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
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 comments on commit 8e47e9f

Please sign in to comment.