|
22 | 22 | use Symfony\Component\Form\FormConfigBuilder;
|
23 | 23 | use Symfony\Component\Form\FormError;
|
24 | 24 | use Symfony\Component\Form\FormInterface;
|
| 25 | +use Symfony\Component\Form\FormRenderer; |
25 | 26 | use Symfony\Component\PropertyAccess\PropertyPath;
|
26 | 27 | use Symfony\Component\Validator\ConstraintViolation;
|
27 | 28 | use Symfony\Component\Validator\ConstraintViolationInterface;
|
| 29 | +use Symfony\Contracts\Translation\TranslatorInterface; |
28 | 30 |
|
29 | 31 | /**
|
30 | 32 | * @author Bernhard Schussek <bschussek@gmail.com>
|
@@ -1565,4 +1567,74 @@ public function testBacktrackIfSeveralSubFormsWithSamePropertyPath()
|
1565 | 1567 | $this->assertEquals([$this->getFormError($violation2, $grandChild2)], iterator_to_array($grandChild2->getErrors()), $grandChild2->getName().' should have an error, but has none');
|
1566 | 1568 | $this->assertEquals([$this->getFormError($violation3, $grandChild3)], iterator_to_array($grandChild3->getErrors()), $grandChild3->getName().' should have an error, but has none');
|
1567 | 1569 | }
|
| 1570 | + |
| 1571 | + public function testMessageWithLabel1() |
| 1572 | + { |
| 1573 | + $renderer = $this->getMockBuilder(FormRenderer::class) |
| 1574 | + ->setMethods(null) |
| 1575 | + ->disableOriginalConstructor() |
| 1576 | + ->getMock() |
| 1577 | + ; |
| 1578 | + $translator = $this->getMockBuilder(TranslatorInterface::class)->getMock(); |
| 1579 | + $translator->expects($this->any())->method('trans')->willReturnMap([ |
| 1580 | + ['Name', [], null, null, 'Custom Name'], |
| 1581 | + ]); |
| 1582 | + $this->mapper = new ViolationMapper($renderer, $translator); |
| 1583 | + |
| 1584 | + $parent = $this->getForm('parent'); |
| 1585 | + $child = $this->getForm('name', 'name'); |
| 1586 | + $parent->add($child); |
| 1587 | + |
| 1588 | + $parent->submit([]); |
| 1589 | + |
| 1590 | + $violation = new ConstraintViolation('Message {{ label }}', null, [], null, 'data.name', null); |
| 1591 | + $this->mapper->mapViolation($violation, $parent); |
| 1592 | + |
| 1593 | + $this->assertCount(1, $child->getErrors(), $child->getName().' should have an error, but has none'); |
| 1594 | + |
| 1595 | + $errors = iterator_to_array($child->getErrors()); |
| 1596 | + if (isset($errors[0])) { |
| 1597 | + /** @var FormError $error */ |
| 1598 | + $error = $errors[0]; |
| 1599 | + $this->assertSame('Message Custom Name', $error->getMessage()); |
| 1600 | + } |
| 1601 | + } |
| 1602 | + |
| 1603 | + public function testMessageWithLabel2() |
| 1604 | + { |
| 1605 | + $translator = $this->getMockBuilder(TranslatorInterface::class)->getMock(); |
| 1606 | + $translator->expects($this->any())->method('trans')->willReturnMap([ |
| 1607 | + ['options_label', [], null, null, 'Translated Label'], |
| 1608 | + ]); |
| 1609 | + $this->mapper = new ViolationMapper(null, $translator); |
| 1610 | + |
| 1611 | + $parent = $this->getForm('parent'); |
| 1612 | + |
| 1613 | + $config = new FormConfigBuilder('name', null, $this->dispatcher, [ |
| 1614 | + 'error_mapping' => [], |
| 1615 | + 'label' => 'options_label', |
| 1616 | + ]); |
| 1617 | + $config->setMapped(true); |
| 1618 | + $config->setInheritData(false); |
| 1619 | + $config->setPropertyPath('name'); |
| 1620 | + $config->setCompound(true); |
| 1621 | + $config->setDataMapper(new PropertyPathMapper()); |
| 1622 | + |
| 1623 | + $child = new Form($config); |
| 1624 | + $parent->add($child); |
| 1625 | + |
| 1626 | + $parent->submit([]); |
| 1627 | + |
| 1628 | + $violation = new ConstraintViolation('Message {{ label }}', null, [], null, 'data.name', null); |
| 1629 | + $this->mapper->mapViolation($violation, $parent); |
| 1630 | + |
| 1631 | + $this->assertCount(1, $child->getErrors(), $child->getName().' should have an error, but has none'); |
| 1632 | + |
| 1633 | + $errors = iterator_to_array($child->getErrors()); |
| 1634 | + if (isset($errors[0])) { |
| 1635 | + /** @var FormError $error */ |
| 1636 | + $error = $errors[0]; |
| 1637 | + $this->assertSame('Message Translated Label', $error->getMessage()); |
| 1638 | + } |
| 1639 | + } |
1568 | 1640 | }
|
0 commit comments