Skip to content

Commit de52f9d

Browse files
committed
Taking into account label_format option
1 parent a86790f commit de52f9d

File tree

2 files changed

+93
-1
lines changed

2 files changed

+93
-1
lines changed

src/Symfony/Component/Form/Extension/Validator/ViolationMapper/ViolationMapper.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,23 @@ public function mapViolation(ConstraintViolation $violation, FormInterface $form
131131

132132
// Only add the error if the form is synchronized
133133
if ($this->acceptsErrors($scope)) {
134-
$label = $scope->getConfig()->getOption('label');
134+
$labelFormat = $scope->getConfig()->getOption('label_format');
135+
136+
if (null !== $labelFormat) {
137+
$label = str_replace(
138+
[
139+
'%name%',
140+
'%id%',
141+
],
142+
[
143+
$scope->getName(),
144+
(string)$scope->getPropertyPath(),
145+
],
146+
$labelFormat
147+
);
148+
} else {
149+
$label = $scope->getConfig()->getOption('label');
150+
}
135151

136152
if (null === $label && null !== $this->formRenderer) {
137153
$label = $this->formRenderer->humanize($scope->getName());

src/Symfony/Component/Form/Tests/Extension/Validator/ViolationMapper/ViolationMapperTest.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1662,4 +1662,80 @@ public function testMessageWithLabel2()
16621662
$this->assertSame('Message Translated Label', $error->getMessage());
16631663
}
16641664
}
1665+
1666+
public function testMessageWithLabelFormat1()
1667+
{
1668+
$translator = $this->getMockBuilder(TranslatorInterface::class)->getMock();
1669+
$translator->expects($this->any())->method('trans')->willReturnMap([
1670+
['form.custom', [], null, null, 'Translated 1st Custom Label'],
1671+
]);
1672+
$this->mapper = new ViolationMapper(null, $translator);
1673+
1674+
$parent = $this->getForm('parent');
1675+
1676+
$config = new FormConfigBuilder('custom', null, $this->dispatcher, [
1677+
'error_mapping' => [],
1678+
'label_format' => 'form.%name%',
1679+
]);
1680+
$config->setMapped(true);
1681+
$config->setInheritData(false);
1682+
$config->setPropertyPath('custom');
1683+
$config->setCompound(true);
1684+
$config->setDataMapper(new PropertyPathMapper());
1685+
1686+
$child = new Form($config);
1687+
$parent->add($child);
1688+
1689+
$parent->submit([]);
1690+
1691+
$violation = new ConstraintViolation('Message {{ label }}', null, [], null, 'data.custom', null);
1692+
$this->mapper->mapViolation($violation, $parent);
1693+
1694+
$this->assertCount(1, $child->getErrors(), $child->getName().' should have an error, but has none');
1695+
1696+
$errors = iterator_to_array($child->getErrors());
1697+
if (isset($errors[0])) {
1698+
/** @var FormError $error */
1699+
$error = $errors[0];
1700+
$this->assertSame('Message Translated 1st Custom Label', $error->getMessage());
1701+
}
1702+
}
1703+
1704+
public function testMessageWithLabelFormat2()
1705+
{
1706+
$translator = $this->getMockBuilder(TranslatorInterface::class)->getMock();
1707+
$translator->expects($this->any())->method('trans')->willReturnMap([
1708+
['form_custom-id', [], null, null, 'Translated 2nd Custom Label'],
1709+
]);
1710+
$this->mapper = new ViolationMapper(null, $translator);
1711+
1712+
$parent = $this->getForm('parent');
1713+
1714+
$config = new FormConfigBuilder('custom-id', null, $this->dispatcher, [
1715+
'error_mapping' => [],
1716+
'label_format' => 'form_%id%',
1717+
]);
1718+
$config->setMapped(true);
1719+
$config->setInheritData(false);
1720+
$config->setPropertyPath('custom-id');
1721+
$config->setCompound(true);
1722+
$config->setDataMapper(new PropertyPathMapper());
1723+
1724+
$child = new Form($config);
1725+
$parent->add($child);
1726+
1727+
$parent->submit([]);
1728+
1729+
$violation = new ConstraintViolation('Message {{ label }}', null, [], null, 'data.custom-id', null);
1730+
$this->mapper->mapViolation($violation, $parent);
1731+
1732+
$this->assertCount(1, $child->getErrors(), $child->getName().' should have an error, but has none');
1733+
1734+
$errors = iterator_to_array($child->getErrors());
1735+
if (isset($errors[0])) {
1736+
/** @var FormError $error */
1737+
$error = $errors[0];
1738+
$this->assertSame('Message Translated 2nd Custom Label', $error->getMessage());
1739+
}
1740+
}
16651741
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy