From d8efc93be09deba86fd8b21d480f53724dc343c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Thu, 10 Mar 2016 21:44:13 +0100 Subject: [PATCH] add form to update/create address and pick postal code --- Entity/Address.php | 13 ++++-- Form/Type/AddressType.php | 55 ++++++++++++++++++++++ Form/Type/PostalCodeType.php | 64 ++++++++++++++++++++++++++ Resources/config/services.yml | 7 +++ Resources/translations/messages.fr.yml | 6 +++ 5 files changed, 141 insertions(+), 4 deletions(-) create mode 100644 Form/Type/AddressType.php create mode 100644 Form/Type/PostalCodeType.php diff --git a/Entity/Address.php b/Entity/Address.php index 2582060d0..792535a17 100644 --- a/Entity/Address.php +++ b/Entity/Address.php @@ -15,12 +15,12 @@ class Address /** * @var string */ - private $streetAddress1; + private $streetAddress1 = ''; /** * @var string */ - private $streetAddress2; + private $streetAddress2 = ''; /** * @var \Chill\MainBundle\Entity\PostalCode @@ -32,6 +32,11 @@ class Address * @var \DateTime */ private $validFrom; + + public function __construct() + { + $this->validFrom = new \DateTime(); + } /** @@ -53,7 +58,7 @@ class Address */ public function setStreetAddress1($streetAddress1) { - $this->streetAddress1 = $streetAddress1; + $this->streetAddress1 = $streetAddress1 === NULL ? '' : $streetAddress1; return $this; } @@ -77,7 +82,7 @@ class Address */ public function setStreetAddress2($streetAddress2) { - $this->streetAddress2 = $streetAddress2; + $this->streetAddress2 = $streetAddress2 === NULL ? '' : $streetAddress2; return $this; } diff --git a/Form/Type/AddressType.php b/Form/Type/AddressType.php new file mode 100644 index 000000000..edb1a657d --- /dev/null +++ b/Form/Type/AddressType.php @@ -0,0 +1,55 @@ + + * + * 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\Type; + +use Symfony\Component\Form\AbstractType; +use Symfony\Component\OptionsResolver\OptionsResolver; +use Symfony\Component\Form\FormBuilderInterface; +use Symfony\Component\Form\Extension\Core\Type\TextType; + +/** + * A type to create/update Address entity + * + * @author Julien Fastré + * @author Champs Libres + */ +class AddressType extends AbstractType +{ + public function buildForm(FormBuilderInterface $builder, array $options) + { + $builder + ->add('streetAddress1', TextType::class, array( + 'required' => true + )) + ->add('streetAddress2', TextType::class, array( + 'required' => false + )) + ->add('postCode', PostalCodeType::class, array( + 'label' => 'Postal code' + )) + ->add('validFrom', 'date', array( + 'required' => true, + 'widget' => 'single_text', + 'format' => 'dd-MM-yyyy' + ) + ) + ; + } +} diff --git a/Form/Type/PostalCodeType.php b/Form/Type/PostalCodeType.php new file mode 100644 index 000000000..8e8655df6 --- /dev/null +++ b/Form/Type/PostalCodeType.php @@ -0,0 +1,64 @@ + + * + * 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\Type; + +use Symfony\Component\Form\AbstractType; +use Symfony\Component\OptionsResolver\OptionsResolver; +use Symfony\Component\Form\FormBuilderInterface; +use Chill\MainBundle\Templating\TranslatableStringHelper; +use Chill\MainBundle\Entity\PostalCode; + +/** + * A form to pick between PostalCode + * + * @author Julien Fastré + * @author Champs Libres + */ +class PostalCodeType extends AbstractType +{ + /** + * + * @var TranslatableStringHelper + */ + protected $translatableStringHelper; + + public function __construct(TranslatableStringHelper $helper) + { + $this->translatableStringHelper = $helper; + } + + + public function getParent() + { + return \Symfony\Bridge\Doctrine\Form\Type\EntityType::class; + } + + public function configureOptions(OptionsResolver $resolver) + { + // create a local copy for usage in Closure + $helper = $this->translatableStringHelper; + $resolver->setDefault('class', PostalCode::class) + ->setDefault('choice_label', function(PostalCode $code) use ($helper) { + return $code->getCode().' '.$code->getName().' ['. + $helper->localize($code->getCountry()->getName()).']'; + } + ); + } +} diff --git a/Resources/config/services.yml b/Resources/config/services.yml index ed3742514..204e7feb4 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -127,3 +127,10 @@ services: - "@translator" tags: - { name: validator.constraint_validator, alias: 'role_scope_scope_presence' } + + chill.main.form.type.postal_code_type: + class: Chill\MainBundle\Form\Type\PostalCodeType + arguments: + - "@chill.main.helper.translatable_string" + tags: + - { name: form.type } diff --git a/Resources/translations/messages.fr.yml b/Resources/translations/messages.fr.yml index 422d49c18..51f355027 100644 --- a/Resources/translations/messages.fr.yml +++ b/Resources/translations/messages.fr.yml @@ -22,6 +22,12 @@ Edit: Modifier Update: Mettre à jour Back to the list: Retour à la liste +#addresses +Street address1: Adresse ligne 1 +Street address2: Adresse ligne 2 +Postal code: Code postal +Valid from: Valide à partir du + #serach Your search is empty. Please provide search terms.: La recherche est vide. Merci de fournir des termes de recherche. The domain %domain% is unknow. Please check your search.: Le domaine de recherche "%domain%" est inconnu. Merci de vérifier votre recherche.