Skip to content

Commit

Permalink
Merge pull request #10 from OWS/feature/hidden_fields_on_form
Browse files Browse the repository at this point in the history
Hide hidden fields on form from constraint
  • Loading branch information
daften authored Oct 9, 2020
2 parents f8002b0 + 0db82c9 commit 006d381
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
54 changes: 51 additions & 3 deletions Form/EventListener/AddressEmbeddableTypeSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@
use CommerceGuys\Addressing\Country\CountryRepository;
use CommerceGuys\Addressing\Country\CountryRepositoryInterface;
use Daften\Bundle\AddressingBundle\Entity\AddressEmbeddable;
use Daften\Bundle\AddressingBundle\Validator\Constraints\EmbeddedAddressFormatConstraint;
use Doctrine\Common\Persistence\ObjectRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Validator\Exception\NoSuchMetadataException;
use Symfony\Component\Validator\Mapping\PropertyMetadataInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;

class AddressEmbeddableTypeSubscriber implements EventSubscriberInterface
{
Expand All @@ -39,6 +44,11 @@ class AddressEmbeddableTypeSubscriber implements EventSubscriberInterface
*/
private $formFactory;

/**
* @var ?ValidatorInterface
*/
private $validator;

/**
* @param CountryRepositoryInterface $countryRepository
* @param FormFactoryInterface $factory
Expand All @@ -47,12 +57,14 @@ public function __construct(
FormFactoryInterface $factory,
CountryRepositoryInterface $countryRepository,
AddressFormatRepositoryInterface $addressFormatRepository,
SubdivisionRepositoryInterface $subdivisionRepository
SubdivisionRepositoryInterface $subdivisionRepository,
ValidatorInterface $validator = null
) {
$this->formFactory = $factory;
$this->countryRepository = $countryRepository;
$this->addressFormatRepository = $addressFormatRepository;
$this->subdivisionRepository = $subdivisionRepository;
$this->validator = $validator;
}

/**
Expand Down Expand Up @@ -101,7 +113,7 @@ public function preSetData(FormEvent $event): void
],
];
}
foreach (AddressFormatHelper::getGroupedFields($addressFormat->getFormat()) as $line_index => $line_fields) {
foreach (AddressFormatHelper::getGroupedFields($addressFormat->getFormat(), $this->getFieldOverrides($form)) as $line_index => $line_fields) {
foreach ($line_fields as $field_index => $field) {
$form->add(
$field,
Expand Down Expand Up @@ -142,7 +154,7 @@ public function preSubmit(FormEvent $event): void
$form->remove($field);
}

foreach (AddressFormatHelper::getGroupedFields($addressFormat->getFormat()) as $line_index => $line_fields) {
foreach (AddressFormatHelper::getGroupedFields($addressFormat->getFormat(), $this->getFieldOverrides($form)) as $line_index => $line_fields) {
foreach ($line_fields as $field_index => $field) {
$form->add($field);
}
Expand All @@ -153,4 +165,40 @@ public function preSubmit(FormEvent $event): void
$form->remove($field);
}
}

private function getFieldOverrides(FormInterface $form)
{
if (!$this->validator) {
return null;
}

$formParent = $form->getParent();
if (!$formParent) {
return null;
}

$parentEntity = $formParent->getData();
if (!is_object($parentEntity)) {
return null;
}

try {
$metadata = $this->validator->getMetadataFor(get_class($parentEntity));
} catch (NoSuchMetadataException $e) {
return null;
}

$propertyMetadatas = $metadata->getPropertyMetadata($form->getName());
/** @var PropertyMetadataInterface $propertyMetadata */
foreach ($propertyMetadatas as $propertyMetadata) {
$constraints = $propertyMetadata->getConstraints();
foreach ($constraints as $constraint) {
if ($constraint instanceof EmbeddedAddressFormatConstraint) {
return $constraint->fieldOverrides;
}
}
}

return null;
}
}
1 change: 1 addition & 0 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ services:
- '@commerceguys.addressing.country.country_repository'
- '@commerceguys.addressing.address_format.address_format_repository'
- '@commerceguys.addressing.subdivision.subdivision_repository'
- '@?validator'

daften.form.type.address:
class: Daften\Bundle\AddressingBundle\Form\Type\AddressEmbeddableType
Expand Down

0 comments on commit 006d381

Please sign in to comment.