add option 'null_if_empty' on AddressType

This commit is contained in:
2019-04-02 12:03:39 +02:00
parent 1ea90c2d5f
commit d915239a24
6 changed files with 149 additions and 6 deletions

View File

@@ -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é <julien.fastre@champs-libres.coop>
* @author Champs Libres <info@champs-libres.coop>
* 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')
;
}
}