From 1f250d7b61195478070a59b2aa8e2e39f10a583a Mon Sep 17 00:00:00 2001 From: WedgeSama Date: Mon, 5 Aug 2013 16:10:37 +0100 Subject: [PATCH 1/3] Select2 default configuration Add the possibility to set up some default options of Select2 in the config.yml I add this possibility cause of the change from 4127f1e. I am using the element default value for many form in some project, so I've got some bugs. I think is better to have the possibility to change the default value. --- DependencyInjection/Configuration.php | 36 +++++++++++++++++++++ DependencyInjection/GenemuFormExtension.php | 1 + Form/JQuery/Type/Select2Type.php | 13 ++++---- Resources/doc/jquery/select2/config.md | 27 ++++++++++++++++ Resources/doc/jquery/select2/index.md | 3 +- 5 files changed, 73 insertions(+), 7 deletions(-) create mode 100644 Resources/doc/jquery/select2/config.md diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index c83a0ed2..9a129d53 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -359,6 +359,42 @@ private function addSelect2(ArrayNodeDefinition $rootNode) ->addDefaultsIfNotSet() ->children() ->booleanNode('enabled')->defaultFalse()->end() + ->arrayNode('configs') + ->addDefaultsIfNotSet() + ->children() + ->scalarNode('placeholder') + ->defaultValue('Select a value') + ->end() + ->scalarNode('width') + ->defaultValue('off') + ->validate() + ->ifNotInArray(array('off', 'element', 'copy', 'resolve')) + ->thenInvalid('Invalid default value "%s"') + ->end() + ->end() + ->booleanNode('allowClear') + ->defaultFalse() + ->end() + ->integerNode('minimumInputLength') + ->defaultValue(0) + ->min(0) + ->end() + ->integerNode('maximumInputLength') + ->min(0) + ->end() + ->scalarNode('separator')->end() + ->integerNode('minimumResultsForSearch') + ->min(1) + ->end() + ->booleanNode('closeOnSelect')->end() + ->booleanNode('openOnEnter')->end() + ->booleanNode('dropdownAutoWidth')->end() + ->booleanNode('selectOnBlur')->end() + ->integerNode('loadMorePadding') + ->min(0) + ->end() + ->end() + ->end() ->end() ->end() ->end() diff --git a/DependencyInjection/GenemuFormExtension.php b/DependencyInjection/GenemuFormExtension.php index 8108c087..603ddfca 100644 --- a/DependencyInjection/GenemuFormExtension.php +++ b/DependencyInjection/GenemuFormExtension.php @@ -258,6 +258,7 @@ private function registerSelect2Configuration(array $configs, ContainerBuilder $ $typeDef ->addArgument($type) ->addTag('form.type', array('alias' => 'genemu_jqueryselect2_'.$type)) + ->addMethodCall('initConfig', array($configs['configs'])) ; $container->setDefinition($serviceId.'.'.$type, $typeDef); diff --git a/Form/JQuery/Type/Select2Type.php b/Form/JQuery/Type/Select2Type.php index 16cb77e2..aded2d50 100644 --- a/Form/JQuery/Type/Select2Type.php +++ b/Form/JQuery/Type/Select2Type.php @@ -28,11 +28,17 @@ class Select2Type extends AbstractType { private $widget; + + private $configs; public function __construct($widget) { $this->widget = $widget; } + + public function initConfig(array $configs) { + $this->configs = $configs; + } /** * {@inheritdoc} @@ -67,12 +73,7 @@ public function buildView(FormView $view, FormInterface $form, array $options) */ public function setDefaultOptions(OptionsResolverInterface $resolver) { - $defaults = array( - 'placeholder' => 'Select a value', - 'allowClear' => false, - 'minimumInputLength' => 0, - 'width' => 'off', - ); + $defaults = $this->configs; $resolver ->setDefaults(array( 'configs' => $defaults, diff --git a/Resources/doc/jquery/select2/config.md b/Resources/doc/jquery/select2/config.md new file mode 100644 index 00000000..dfdba1f5 --- /dev/null +++ b/Resources/doc/jquery/select2/config.md @@ -0,0 +1,27 @@ +# jQuery Select2: Set the default configuration + +### Default configuration: +``` yml +# app/config/config.yml +genemu_form: + select2: + enabled: true + configs: + placeholder: Select a value + width: off + allowClear: false + minimumInputLength: 0 +``` + +### Other options: +- maximumInputLength +- separator +- minimumResultsForSearch +- closeOnSelect +- openOnEnter +- dropdownAutoWidth +- selectOnBlur +- loadMorePadding + +see officiel select2's documentation for more information: +http://ivaynberg.github.io/select2/ diff --git a/Resources/doc/jquery/select2/index.md b/Resources/doc/jquery/select2/index.md index 0d195f26..9a48aab5 100644 --- a/Resources/doc/jquery/select2/index.md +++ b/Resources/doc/jquery/select2/index.md @@ -42,4 +42,5 @@ documentation. ## Extra -1. [Ajax](https://github.com/genemu/GenemuFormBundle/blob/master/Resources/doc/jquery/select2/ajax.md) \ No newline at end of file +1. [Ajax](https://github.com/genemu/GenemuFormBundle/blob/master/Resources/doc/jquery/select2/ajax.md) +2. [Default configuration](https://github.com/genemu/GenemuFormBundle/blob/master/Resources/doc/jquery/select2/config.md) \ No newline at end of file From 798502f0e689159ae15282250ab087626ac9846a Mon Sep 17 00:00:00 2001 From: WedgeSama Date: Wed, 7 Aug 2013 12:39:40 +0100 Subject: [PATCH 2/3] Array prototype for select2 config --- DependencyInjection/Configuration.php | 34 +------------------------- Resources/doc/jquery/select2/config.md | 18 ++++---------- Resources/doc/jquery/select2/index.md | 2 +- 3 files changed, 7 insertions(+), 47 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 9a129d53..079df24d 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -360,39 +360,7 @@ private function addSelect2(ArrayNodeDefinition $rootNode) ->children() ->booleanNode('enabled')->defaultFalse()->end() ->arrayNode('configs') - ->addDefaultsIfNotSet() - ->children() - ->scalarNode('placeholder') - ->defaultValue('Select a value') - ->end() - ->scalarNode('width') - ->defaultValue('off') - ->validate() - ->ifNotInArray(array('off', 'element', 'copy', 'resolve')) - ->thenInvalid('Invalid default value "%s"') - ->end() - ->end() - ->booleanNode('allowClear') - ->defaultFalse() - ->end() - ->integerNode('minimumInputLength') - ->defaultValue(0) - ->min(0) - ->end() - ->integerNode('maximumInputLength') - ->min(0) - ->end() - ->scalarNode('separator')->end() - ->integerNode('minimumResultsForSearch') - ->min(1) - ->end() - ->booleanNode('closeOnSelect')->end() - ->booleanNode('openOnEnter')->end() - ->booleanNode('dropdownAutoWidth')->end() - ->booleanNode('selectOnBlur')->end() - ->integerNode('loadMorePadding') - ->min(0) - ->end() + ->prototype('variable') ->end() ->end() ->end() diff --git a/Resources/doc/jquery/select2/config.md b/Resources/doc/jquery/select2/config.md index dfdba1f5..9e5cc9c9 100644 --- a/Resources/doc/jquery/select2/config.md +++ b/Resources/doc/jquery/select2/config.md @@ -1,6 +1,8 @@ -# jQuery Select2: Set the default configuration +# jQuery Select2 configuration -### Default configuration: +By default, just use the default configuration of select2. + +### Configuration example: ``` yml # app/config/config.yml genemu_form: @@ -13,15 +15,5 @@ genemu_form: minimumInputLength: 0 ``` -### Other options: -- maximumInputLength -- separator -- minimumResultsForSearch -- closeOnSelect -- openOnEnter -- dropdownAutoWidth -- selectOnBlur -- loadMorePadding - see officiel select2's documentation for more information: -http://ivaynberg.github.io/select2/ +http://ivaynberg.github.io/select2/#documentation diff --git a/Resources/doc/jquery/select2/index.md b/Resources/doc/jquery/select2/index.md index 9a48aab5..9241963c 100644 --- a/Resources/doc/jquery/select2/index.md +++ b/Resources/doc/jquery/select2/index.md @@ -43,4 +43,4 @@ documentation. ## Extra 1. [Ajax](https://github.com/genemu/GenemuFormBundle/blob/master/Resources/doc/jquery/select2/ajax.md) -2. [Default configuration](https://github.com/genemu/GenemuFormBundle/blob/master/Resources/doc/jquery/select2/config.md) \ No newline at end of file +2. [Configuration example](https://github.com/genemu/GenemuFormBundle/blob/master/Resources/doc/jquery/select2/config.md) \ No newline at end of file From 5cffe76e09c6a0a6babc614d11a02e03d5bee161 Mon Sep 17 00:00:00 2001 From: WedgeSama Date: Wed, 7 Aug 2013 14:53:24 +0100 Subject: [PATCH 3/3] Select2Type initConfig remove, use constructor --- DependencyInjection/GenemuFormExtension.php | 2 +- Form/JQuery/Type/Select2Type.php | 5 +---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/DependencyInjection/GenemuFormExtension.php b/DependencyInjection/GenemuFormExtension.php index 603ddfca..e6f54b5b 100644 --- a/DependencyInjection/GenemuFormExtension.php +++ b/DependencyInjection/GenemuFormExtension.php @@ -257,8 +257,8 @@ private function registerSelect2Configuration(array $configs, ContainerBuilder $ $typeDef = new DefinitionDecorator($serviceId); $typeDef ->addArgument($type) + ->addArgument($configs['configs']) ->addTag('form.type', array('alias' => 'genemu_jqueryselect2_'.$type)) - ->addMethodCall('initConfig', array($configs['configs'])) ; $container->setDefinition($serviceId.'.'.$type, $typeDef); diff --git a/Form/JQuery/Type/Select2Type.php b/Form/JQuery/Type/Select2Type.php index aded2d50..595a31ce 100644 --- a/Form/JQuery/Type/Select2Type.php +++ b/Form/JQuery/Type/Select2Type.php @@ -31,12 +31,9 @@ class Select2Type extends AbstractType private $configs; - public function __construct($widget) + public function __construct($widget, array $configs = array()) { $this->widget = $widget; - } - - public function initConfig(array $configs) { $this->configs = $configs; }