add form to update/create address and pick postal code

This commit is contained in:
Julien Fastré 2016-03-10 21:44:13 +01:00
parent a9e04e56be
commit d8efc93be0
5 changed files with 141 additions and 4 deletions

View File

@ -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;
}

55
Form/Type/AddressType.php Normal file
View File

@ -0,0 +1,55 @@
<?php
/*
* Copyright (C) 2016 Champs-Libres <info@champs-libres.coop>
*
* 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 <http://www.gnu.org/licenses/>.
*/
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é <julien.fastre@champs-libres.coop>
* @author Champs Libres <info@champs-libres.coop>
*/
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'
)
)
;
}
}

View File

@ -0,0 +1,64 @@
<?php
/*
* Copyright (C) 2016 Champs-Libres <info@champs-libres.coop>
*
* 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 <http://www.gnu.org/licenses/>.
*/
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é <julien.fastre@champs-libres.coop>
* @author Champs Libres <info@champs-libres.coop>
*/
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()).']';
}
);
}
}

View File

@ -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 }

View File

@ -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.