Skip to content

Instantly share code, notes, and snippets.

@ger86
Created April 3, 2019 17:00
Show Gist options
  • Save ger86/e0c6f8af5a9bf0be86f3770a18ea23bc to your computer and use it in GitHub Desktop.
Save ger86/e0c6f8af5a9bf0be86f3770a18ea23bc to your computer and use it in GitHub Desktop.
<?php
namespace App\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\FormInterface;
class Select2Type extends AbstractType {
public function buildView(FormView $view, FormInterface $form, array $options)
{
$attr = $view->vars['attr'];
$class = isset($attr['class']) ? $attr['class'].' ' : '';
$class .= 'select2';
$attr['class'] = $class;
$attr['data-autocomplete-url'] = $attr['data-autocomplete-url'];
$view->vars['attr'] = $attr;
}
public function getParent() {
return ChoiceType::class;
}
public function configureOptions(OptionsResolver $resolver) {
$resolver->setDefaults([
'attr' => [
'class' => 'select2',
'data-autocomplete-url' => ''
]
]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment