diff --git a/CHANGELOG.md b/CHANGELOG.md index 3d7ae6744..7b1727407 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,3 +35,8 @@ Version 1.5.5 - add api for grouping centers ; - select centers as grouped on step "pick centers" in exports ; +Master branch +============= + +- add an option to allow address to be empty if street or postcode is not set. Used for embedding address in another form, when address is not required. + diff --git a/Entity/Address.php b/Entity/Address.php index 49951e48f..91390fc1a 100644 --- a/Entity/Address.php +++ b/Entity/Address.php @@ -28,6 +28,8 @@ class Address private $postcode; /** + * Indicates when the address starts validation. Used to build an history + * of address. By default, the current date. * * @var \DateTime */ diff --git a/Form/ChoiceLoader/PostalCodeChoiceLoader.php b/Form/ChoiceLoader/PostalCodeChoiceLoader.php index 71ebbd919..4b52ba38c 100644 --- a/Form/ChoiceLoader/PostalCodeChoiceLoader.php +++ b/Form/ChoiceLoader/PostalCodeChoiceLoader.php @@ -59,7 +59,11 @@ class PostalCodeChoiceLoader implements ChoiceLoaderInterface $choices = []; foreach($values as $value) { - $choices[] = $this->postalCodeRepository->find($value); + if (empty($value)) { + $choices[] = null; + } else { + $choices[] = $this->postalCodeRepository->find($value); + } } return $choices; diff --git a/Form/DataMapper/AddressDataMapper.php b/Form/DataMapper/AddressDataMapper.php new file mode 100644 index 000000000..657ed7636 --- /dev/null +++ b/Form/DataMapper/AddressDataMapper.php @@ -0,0 +1,108 @@ + + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +namespace Chill\MainBundle\Form\DataMapper; + +use Symfony\Component\Form\DataMapperInterface; +use Chill\MainBundle\Entity\Address; +use Chill\MainBundle\Entity\PostalCode; +use Symfony\Component\Form\FormInterface; +use Symfony\Component\Form\Exception\UnexpectedTypeException; + +/** + * Add a data mapper to Address. + * + * If the address is incomplete, the data mapper returns null + */ +class AddressDataMapper implements DataMapperInterface +{ + /** + * + * @param Address $address + * @param \Iterator $forms + */ + public function mapDataToForms($address, $forms) + { + if (NULL === $address) { + return; + } + + if (!$address instanceof Address) { + throw new UnexpectedTypeException($address, Address::class); + } + + foreach ($forms as $key => $form) { + /** @var FormInterface $form */ + switch ($key) { + case 'streetAddress1': + $form->setData($address->getStreetAddress1()); + break; + case 'streetAddress2': + $form->setData($address->getStreetAddress2()); + break; + case 'postCode': + $form->setData($address->getPostcode()); + break; + case 'validFrom': + $form->setData($address->getValidFrom()); + break; + default: + break; + } + } + } + + /** + * + * @param \Iterator $forms + * @param Address $address + */ + public function mapFormsToData($forms, &$address) + { + if (!$address instanceof Address) { + $address = new Address(); + } + + foreach ($forms as $key => $form) { + /** @var FormInterface $form */ + switch($key) { + case 'postCode': + if (!$form->getData() instanceof PostalCode) { + $address = null; + return; + } + $address->setPostcode($form->getData()); + break; + case 'streetAddress1': + if (empty($form->getData())) { + $address = null; + return; + } + $address->setStreetAddress1($form->getData()); + break; + case 'streetAddress2': + $address->setStreetAddress2($form->getData()); + break; + case 'validFrom': + $address->setValidFrom($form->getData()); + break; + default: + break; + } + } + } +} diff --git a/Form/Type/AddressType.php b/Form/Type/AddressType.php index ee15e3d20..80a29cf5b 100644 --- a/Form/Type/AddressType.php +++ b/Form/Type/AddressType.php @@ -26,12 +26,18 @@ use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\Extension\Core\Type\DateType; use Chill\MainBundle\Entity\Address; use Chill\MainBundle\Form\Type\PostalCodeType; +use Chill\MainBundle\Form\DataMapper\AddressDataMapper; /** * A type to create/update Address entity * - * @author Julien Fastré - * @author Champs Libres + * Options: + * + * - `has_valid_from` (boolean): show if an entry "has valid from" must be + * shown. + * - `null_if_empty` (boolean): replace the address type by null if the street + * or the postCode is empty. This is useful when the address is not required and + * embedded in another form. */ class AddressType extends AbstractType { @@ -49,17 +55,33 @@ class AddressType extends AbstractType 'placeholder' => 'Choose a postal code', 'required' => true )) + ; + + if ($options['has_valid_from']) { + $builder ->add('validFrom', DateType::class, array( 'required' => true, 'widget' => 'single_text', 'format' => 'dd-MM-yyyy' ) - ) - ; + ); + } + + if ($options['null_if_emtpy'] === TRUE) { + $builder->setDataMapper(new AddressDataMapper()); + } } public function configureOptions(OptionsResolver $resolver) { - $resolver->setDefault('data_class', Address::class); + $resolver + ->setDefault('data_class', Address::class) + ->setDefined('has_valid_from') + ->setAllowedTypes('has_valid_from', 'bool') + ->setDefault('has_valid_from', true) + ->setDefined('null_if_empty') + ->setDefault('null_if_empty', false) + ->setAllowedTypes('null_if_empty', 'bool') + ; } } diff --git a/Resources/config/validation.yml b/Resources/config/validation.yml index 5843e0427..f592845de 100644 --- a/Resources/config/validation.yml +++ b/Resources/config/validation.yml @@ -41,6 +41,8 @@ Chill\MainBundle\Entity\Address: - Length: min: 2 max: 250 + - NotNull: ~ + - NotBlank: ~ postcode: - NotNull: ~ validFrom: