Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to override view transformer in Select2Type #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Form/Type/Select2Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -95,6 +95,7 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
);

$defaults = array(
'apply_view_transformer' => true,
'multiple' => false,
'expanded' => false,
'empty_value' => 'select.empty_value',
Expand Down
2 changes: 2 additions & 0 deletions Resources/doc/select2.md
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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:

Expand Down
22 changes: 22 additions & 0 deletions Tests/Form/Type/Select2TypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down