|
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\Form\Tests\Extension\Validator\ViolationMapper\Fixtures\Issue;
|
26 | 27 | use Symfony\Component\PropertyAccess\PropertyPath;
|
27 | 28 | use Symfony\Component\Validator\ConstraintViolation;
|
28 | 29 | use Symfony\Component\Validator\ConstraintViolationInterface;
|
| 30 | +use Symfony\Contracts\Translation\TranslatorInterface; |
29 | 31 |
|
30 | 32 | /**
|
31 | 33 | * @author Bernhard Schussek <bschussek@gmail.com>
|
@@ -1590,4 +1592,150 @@ public function testBacktrackIfSeveralSubFormsWithSamePropertyPath()
|
1590 | 1592 | $this->assertEquals([$this->getFormError($violation2, $grandChild2)], iterator_to_array($grandChild2->getErrors()), $grandChild2->getName().' should have an error, but has none');
|
1591 | 1593 | $this->assertEquals([$this->getFormError($violation3, $grandChild3)], iterator_to_array($grandChild3->getErrors()), $grandChild3->getName().' should have an error, but has none');
|
1592 | 1594 | }
|
| 1595 | + |
| 1596 | + public function testMessageWithLabel1() |
| 1597 | + { |
| 1598 | + $renderer = $this->getMockBuilder(FormRenderer::class) |
| 1599 | + ->setMethods(null) |
| 1600 | + ->disableOriginalConstructor() |
| 1601 | + ->getMock() |
| 1602 | + ; |
| 1603 | + $translator = $this->getMockBuilder(TranslatorInterface::class)->getMock(); |
| 1604 | + $translator->expects($this->any())->method('trans')->willReturnMap([ |
| 1605 | + ['Name', [], null, null, 'Custom Name'], |
| 1606 | + ]); |
| 1607 | + $this->mapper = new ViolationMapper($renderer, $translator); |
| 1608 | + |
| 1609 | + $parent = $this->getForm('parent'); |
| 1610 | + $child = $this->getForm('name', 'name'); |
| 1611 | + $parent->add($child); |
| 1612 | + |
| 1613 | + $parent->submit([]); |
| 1614 | + |
| 1615 | + $violation = new ConstraintViolation('Message {{ label }}', null, [], null, 'data.name', null); |
| 1616 | + $this->mapper->mapViolation($violation, $parent); |
| 1617 | + |
| 1618 | + $this->assertCount(1, $child->getErrors(), $child->getName().' should have an error, but has none'); |
| 1619 | + |
| 1620 | + $errors = iterator_to_array($child->getErrors()); |
| 1621 | + if (isset($errors[0])) { |
| 1622 | + /** @var FormError $error */ |
| 1623 | + $error = $errors[0]; |
| 1624 | + $this->assertSame('Message Custom Name', $error->getMessage()); |
| 1625 | + } |
| 1626 | + } |
| 1627 | + |
| 1628 | + public function testMessageWithLabel2() |
| 1629 | + { |
| 1630 | + $translator = $this->getMockBuilder(TranslatorInterface::class)->getMock(); |
| 1631 | + $translator->expects($this->any())->method('trans')->willReturnMap([ |
| 1632 | + ['options_label', [], null, null, 'Translated Label'], |
| 1633 | + ]); |
| 1634 | + $this->mapper = new ViolationMapper(null, $translator); |
| 1635 | + |
| 1636 | + $parent = $this->getForm('parent'); |
| 1637 | + |
| 1638 | + $config = new FormConfigBuilder('name', null, $this->dispatcher, [ |
| 1639 | + 'error_mapping' => [], |
| 1640 | + 'label' => 'options_label', |
| 1641 | + ]); |
| 1642 | + $config->setMapped(true); |
| 1643 | + $config->setInheritData(false); |
| 1644 | + $config->setPropertyPath('name'); |
| 1645 | + $config->setCompound(true); |
| 1646 | + $config->setDataMapper(new PropertyPathMapper()); |
| 1647 | + |
| 1648 | + $child = new Form($config); |
| 1649 | + $parent->add($child); |
| 1650 | + |
| 1651 | + $parent->submit([]); |
| 1652 | + |
| 1653 | + $violation = new ConstraintViolation('Message {{ label }}', null, [], null, 'data.name', null); |
| 1654 | + $this->mapper->mapViolation($violation, $parent); |
| 1655 | + |
| 1656 | + $this->assertCount(1, $child->getErrors(), $child->getName().' should have an error, but has none'); |
| 1657 | + |
| 1658 | + $errors = iterator_to_array($child->getErrors()); |
| 1659 | + if (isset($errors[0])) { |
| 1660 | + /** @var FormError $error */ |
| 1661 | + $error = $errors[0]; |
| 1662 | + $this->assertSame('Message Translated Label', $error->getMessage()); |
| 1663 | + } |
| 1664 | + } |
| 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 | + } |
1593 | 1741 | }
|
0 commit comments