Skip to content

Commit 006d381

Browse files
authored
Merge pull request #10 from OWS/feature/hidden_fields_on_form
Hide hidden fields on form from constraint
2 parents f8002b0 + 0db82c9 commit 006d381

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

Form/EventListener/AddressEmbeddableTypeSubscriber.php

+51-3
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,16 @@
1111
use CommerceGuys\Addressing\Country\CountryRepository;
1212
use CommerceGuys\Addressing\Country\CountryRepositoryInterface;
1313
use Daften\Bundle\AddressingBundle\Entity\AddressEmbeddable;
14+
use Daften\Bundle\AddressingBundle\Validator\Constraints\EmbeddedAddressFormatConstraint;
1415
use Doctrine\Common\Persistence\ObjectRepository;
1516
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1617
use Symfony\Component\Form\FormEvent;
1718
use Symfony\Component\Form\FormEvents;
1819
use Symfony\Component\Form\FormFactoryInterface;
20+
use Symfony\Component\Form\FormInterface;
21+
use Symfony\Component\Validator\Exception\NoSuchMetadataException;
22+
use Symfony\Component\Validator\Mapping\PropertyMetadataInterface;
23+
use Symfony\Component\Validator\Validator\ValidatorInterface;
1924

2025
class AddressEmbeddableTypeSubscriber implements EventSubscriberInterface
2126
{
@@ -39,6 +44,11 @@ class AddressEmbeddableTypeSubscriber implements EventSubscriberInterface
3944
*/
4045
private $formFactory;
4146

47+
/**
48+
* @var ?ValidatorInterface
49+
*/
50+
private $validator;
51+
4252
/**
4353
* @param CountryRepositoryInterface $countryRepository
4454
* @param FormFactoryInterface $factory
@@ -47,12 +57,14 @@ public function __construct(
4757
FormFactoryInterface $factory,
4858
CountryRepositoryInterface $countryRepository,
4959
AddressFormatRepositoryInterface $addressFormatRepository,
50-
SubdivisionRepositoryInterface $subdivisionRepository
60+
SubdivisionRepositoryInterface $subdivisionRepository,
61+
ValidatorInterface $validator = null
5162
) {
5263
$this->formFactory = $factory;
5364
$this->countryRepository = $countryRepository;
5465
$this->addressFormatRepository = $addressFormatRepository;
5566
$this->subdivisionRepository = $subdivisionRepository;
67+
$this->validator = $validator;
5668
}
5769

5870
/**
@@ -101,7 +113,7 @@ public function preSetData(FormEvent $event): void
101113
],
102114
];
103115
}
104-
foreach (AddressFormatHelper::getGroupedFields($addressFormat->getFormat()) as $line_index => $line_fields) {
116+
foreach (AddressFormatHelper::getGroupedFields($addressFormat->getFormat(), $this->getFieldOverrides($form)) as $line_index => $line_fields) {
105117
foreach ($line_fields as $field_index => $field) {
106118
$form->add(
107119
$field,
@@ -142,7 +154,7 @@ public function preSubmit(FormEvent $event): void
142154
$form->remove($field);
143155
}
144156

145-
foreach (AddressFormatHelper::getGroupedFields($addressFormat->getFormat()) as $line_index => $line_fields) {
157+
foreach (AddressFormatHelper::getGroupedFields($addressFormat->getFormat(), $this->getFieldOverrides($form)) as $line_index => $line_fields) {
146158
foreach ($line_fields as $field_index => $field) {
147159
$form->add($field);
148160
}
@@ -153,4 +165,40 @@ public function preSubmit(FormEvent $event): void
153165
$form->remove($field);
154166
}
155167
}
168+
169+
private function getFieldOverrides(FormInterface $form)
170+
{
171+
if (!$this->validator) {
172+
return null;
173+
}
174+
175+
$formParent = $form->getParent();
176+
if (!$formParent) {
177+
return null;
178+
}
179+
180+
$parentEntity = $formParent->getData();
181+
if (!is_object($parentEntity)) {
182+
return null;
183+
}
184+
185+
try {
186+
$metadata = $this->validator->getMetadataFor(get_class($parentEntity));
187+
} catch (NoSuchMetadataException $e) {
188+
return null;
189+
}
190+
191+
$propertyMetadatas = $metadata->getPropertyMetadata($form->getName());
192+
/** @var PropertyMetadataInterface $propertyMetadata */
193+
foreach ($propertyMetadatas as $propertyMetadata) {
194+
$constraints = $propertyMetadata->getConstraints();
195+
foreach ($constraints as $constraint) {
196+
if ($constraint instanceof EmbeddedAddressFormatConstraint) {
197+
return $constraint->fieldOverrides;
198+
}
199+
}
200+
}
201+
202+
return null;
203+
}
156204
}

Resources/config/services.yml

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ services:
1111
- '@commerceguys.addressing.country.country_repository'
1212
- '@commerceguys.addressing.address_format.address_format_repository'
1313
- '@commerceguys.addressing.subdivision.subdivision_repository'
14+
- '@?validator'
1415

1516
daften.form.type.address:
1617
class: Daften\Bundle\AddressingBundle\Form\Type\AddressEmbeddableType

0 commit comments

Comments
 (0)