mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-23 16:13:50 +00:00
choice with other + some development tips
This commit is contained in:
53
Form/Type/ChoiceWithOtherType.php
Normal file
53
Form/Type/ChoiceWithOtherType.php
Normal 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';
|
||||
}
|
||||
}
|
66
Form/Type/ChoicesListType.php
Normal file
66
Form/Type/ChoicesListType.php
Normal 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
26
Form/Type/ChoicesType.php
Normal 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';
|
||||
}
|
||||
|
||||
|
||||
}
|
Reference in New Issue
Block a user