choice with other + some development tips

This commit is contained in:
2014-11-10 11:25:25 +01:00
parent f9be8e3412
commit 952d70f049
18 changed files with 466 additions and 9 deletions

View File

@@ -0,0 +1,53 @@
<?php
namespace Chill\CustomFieldsBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
/**
* Return a choice widget with an "other" option
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*
*/
class ChoiceWithOtherType extends AbstractType
{
/* (non-PHPdoc)
* @see \Symfony\Component\Form\AbstractType::buildForm()
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
//add an 'other' entry in choices array
$options['choices']['_other'] = '__other__';
//ChoiceWithOther must always be expanded
$options['expanded'] = true;
$builder
->add('_other', 'text', array('required' => false))
->add('_choices', 'choice', $options)
;
}
/* (non-PHPdoc)
* @see \Symfony\Component\Form\AbstractType::setDefaultOptions()
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver
->setRequired(array('choices'))
->setAllowedTypes(array('choices' => array('array')))
->setDefaults(array('multiple' => false))
;
}
public function getName()
{
return 'choice_with_other';
}
}

View File

@@ -0,0 +1,66 @@
<?php
namespace Chill\CustomFieldsBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
class ChoicesListType extends AbstractType
{
private $defaultLocale;
public function __construct($defaultLocale)
{
$this->defaultLocale = $defaultLocale;
}
/* (non-PHPdoc)
* @see \Symfony\Component\Form\AbstractType::buildForm()
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$locale = $this->defaultLocale;
$builder->add('name', 'translatable_string')
->add('active', 'checkbox', array(
'required' => false,
'empty_data' => true
))
->add('slug', 'hidden', array(
))
->addEventListener(FormEvents::SUBMIT, function(FormEvent $event) use ($locale){
$form = $event->getForm();
$data = $event->getData();
$formData = $form->getData();
if (NULL === $formData['slug']) {
$slug = $form['name'][$locale]->getData();
$slug= strtolower($slug);
$slug= preg_replace('/[^a-zA-Z0-9 -]/','', $slug); // only take alphanumerical characters, but keep the spaces and dashes too...
$slug= str_replace(' ','-', $slug); // replace spaces by dashes
$data['slug'] = $slug;
$event->setData($data);
} else {
$data['slug'] = $formData['slug'];
$event->setData($data);
}
})
;
}
/*
*
* @see \Symfony\Component\Form\FormTypeInterface::getName()
*/
public function getName()
{
return 'cf_choices_list';
}
}

26
Form/Type/ChoicesType.php Normal file
View File

@@ -0,0 +1,26 @@
<?php
namespace Chill\CustomFieldsBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
/**
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*
*/
class ChoicesType extends AbstractType
{
public function getName()
{
return 'cf_choices';
}
public function getParent()
{
return 'collection';
}
}