diff --git a/Form/Type/Select2Type.php b/Form/Type/Select2Type.php index 0dad711..4261a34 100644 --- a/Form/Type/Select2Type.php +++ b/Form/Type/Select2Type.php @@ -60,7 +60,7 @@ public function __construct(DataTransformerInterface $transformer, $widget) */ public function buildForm(FormBuilderInterface $builder, array $options) { - if ($this->widget === 'ajax' && $options['multiple'] === true){ + if ($options['apply_view_transformer'] && $this->widget === 'ajax' && $options['multiple'] === true) { $builder->addViewTransformer($this->transformer); } } @@ -95,6 +95,7 @@ public function setDefaultOptions(OptionsResolverInterface $resolver) ); $defaults = array( + 'apply_view_transformer' => true, 'multiple' => false, 'expanded' => false, 'empty_value' => 'select.empty_value', diff --git a/Resources/doc/select2.md b/Resources/doc/select2.md index 7dda1d5..7e14793 100644 --- a/Resources/doc/select2.md +++ b/Resources/doc/select2.md @@ -54,6 +54,7 @@ public function buildForm(FormBuilderInterface $builder, array $options) $builder // ..... ->add('name', 'thrace_select2_ajax', array( + 'apply_view_transformer' => true, 'label' => 'Select', 'multiple' => false, 'empty_value' => 'Select option', @@ -80,6 +81,7 @@ public function buildForm(FormBuilderInterface $builder, array $options) ; } ``` +**Note:** *apply_view_transformer* option enables \Thrace\FormBundle\Form\DataTransformer\ArrayToStringTransformer as the default transformer -only if **multiple** is set true-, setting it **false** allows you to use your own transformer And the array structure: diff --git a/Tests/Form/Type/Select2TypeTest.php b/Tests/Form/Type/Select2TypeTest.php index 688e421..536e988 100644 --- a/Tests/Form/Type/Select2TypeTest.php +++ b/Tests/Form/Type/Select2TypeTest.php @@ -22,6 +22,28 @@ public function testDefaultConfigs() ), $configs); } + public function testAjaxMultipleWithoutViewTransformer() + { + $form = $this->factory->create( + 'thrace_select2_ajax', + null, + array( + 'multiple' => true, + 'configs' => array('ajax' => array(),'apply_view_transformer' => false) + ) + ); + $view = $form->createView(); + $configs = $view->vars['configs']; + $this->assertSame(array( + 'width' => '300px', + 'allowClear' => true, + 'ajax' => array (), + 'apply_view_transformer' => false, + 'placeholder' => 'select.empty_value', + 'multiple' => true, + ), $configs); + } + public function testAjaxAndMultiple() { $form = $this->factory->create('thrace_select2_ajax', null, array(