From d5ac4975762ef79f67a76c2744d4af7c24b59218 Mon Sep 17 00:00:00 2001 From: Ben Davies Date: Thu, 11 Aug 2022 21:52:37 +0100 Subject: [PATCH] Only add the clear_button plugin to TomSelect if the form element is not required we need to add our own `required` attr here, as symfony does not always render the `required` form view variable see: https://github.com/symfony/symfony/blob/b1b77f366d42a58c1cd0879c605b997d05279d01/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig\#L59-L61 --- src/Autocomplete/assets/dist/controller.js | 2 +- src/Autocomplete/assets/src/controller.ts | 2 +- .../src/Form/AutocompleteChoiceTypeExtension.php | 2 ++ src/Autocomplete/tests/Fixtures/Form/ProductType.php | 10 ++++++++++ .../Functional/AutocompleteFormRenderingTest.php | 11 +++++++++++ 5 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/Autocomplete/assets/dist/controller.js b/src/Autocomplete/assets/dist/controller.js index f7536c0036b..b7ac04572d8 100644 --- a/src/Autocomplete/assets/dist/controller.js +++ b/src/Autocomplete/assets/dist/controller.js @@ -219,7 +219,7 @@ class default_1 extends Controller { _default_1_instances = new WeakSet(), _default_1_getCommonConfig = function _default_1_getCommonConfig() { const plugins = {}; const isMultiple = !this.selectElement || this.selectElement.multiple; - if (!this.formElement.disabled && !isMultiple) { + if (!this.formElement.required && !this.formElement.disabled && !isMultiple) { plugins.clear_button = { title: '' }; } if (isMultiple) { diff --git a/src/Autocomplete/assets/src/controller.ts b/src/Autocomplete/assets/src/controller.ts index 8be04e8af14..fcd9360adae 100644 --- a/src/Autocomplete/assets/src/controller.ts +++ b/src/Autocomplete/assets/src/controller.ts @@ -120,7 +120,7 @@ export default class extends Controller { // multiple values excepted if this is NOT A select (i.e. input) or a multiple select const isMultiple = !this.selectElement || this.selectElement.multiple; - if (!this.formElement.disabled && !isMultiple) { + if (!this.formElement.required && !this.formElement.disabled && !isMultiple) { plugins.clear_button = { title: '' }; } diff --git a/src/Autocomplete/src/Form/AutocompleteChoiceTypeExtension.php b/src/Autocomplete/src/Form/AutocompleteChoiceTypeExtension.php index 9c40fd39270..e3e3cf6b52b 100644 --- a/src/Autocomplete/src/Form/AutocompleteChoiceTypeExtension.php +++ b/src/Autocomplete/src/Form/AutocompleteChoiceTypeExtension.php @@ -54,6 +54,8 @@ public function finishView(FormView $view, FormInterface $form, array $options): $attr = $view->vars['attr'] ?? []; + $attr['required'] = $view->vars['required']; + $controllerName = 'symfony--ux-autocomplete--autocomplete'; $attr['data-controller'] = trim(($attr['data-controller'] ?? '').' '.$controllerName); diff --git a/src/Autocomplete/tests/Fixtures/Form/ProductType.php b/src/Autocomplete/tests/Fixtures/Form/ProductType.php index c2c5b35bf18..a9706edad3c 100644 --- a/src/Autocomplete/tests/Fixtures/Form/ProductType.php +++ b/src/Autocomplete/tests/Fixtures/Form/ProductType.php @@ -46,6 +46,16 @@ public function buildForm(FormBuilderInterface $builder, array $options): void 'autocomplete' => true, 'mapped' => false, ]) + ->add('colour', ChoiceType::class, [ + 'choices' => [ + 'red' => 'red', + 'blue' => 'blue', + 'green' => 'green', + ], + 'autocomplete' => true, + 'mapped' => false, + 'required' => false, + ]) ->add('tags', TextType::class, [ 'mapped' => false, 'autocomplete' => true, diff --git a/src/Autocomplete/tests/Functional/AutocompleteFormRenderingTest.php b/src/Autocomplete/tests/Functional/AutocompleteFormRenderingTest.php index 72d801b6f82..f699134d2d9 100644 --- a/src/Autocomplete/tests/Functional/AutocompleteFormRenderingTest.php +++ b/src/Autocomplete/tests/Functional/AutocompleteFormRenderingTest.php @@ -34,10 +34,21 @@ public function testFieldsRenderWithStimulusController() ->assertElementAttributeContains('#product_category', 'data-symfony--ux-autocomplete--autocomplete-url-value', '/test/autocomplete/category_autocomplete_type?extra_options=') ->assertElementAttributeContains('#product_category', 'data-symfony--ux-autocomplete--autocomplete-min-characters-value', '2') ->assertElementAttributeContains('#product_category', 'data-symfony--ux-autocomplete--autocomplete-max-results-value', '25') + ->assertElementAttributeContains('#product_category', 'required', 'required') + + ->assertElementAttributeContains('#product_ingredients', 'required', 'required') ->assertElementAttributeContains('#product_portionSize', 'data-controller', 'symfony--ux-autocomplete--autocomplete') + ->assertElementAttributeContains('#product_portionSize', 'required', 'required') + + ->assertElementAttributeContains('#product_colour', 'data-controller', 'symfony--ux-autocomplete--autocomplete') + // not implemented in zenstruck/browser + // ->elementAttributeNotExists('#product_colour', 'required') + ->assertNotSeeElement('#product_colour[required]') + ->assertElementAttributeContains('#product_tags', 'data-controller', 'symfony--ux-autocomplete--autocomplete') ->assertElementAttributeContains('#product_tags', 'data-symfony--ux-autocomplete--autocomplete-tom-select-options-value', 'createOnBlur') + ->assertElementAttributeContains('#product_tags', 'required', 'required') ; }