From 9d852c7b289b032f94f0039f7a1b02c30e24a067 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/src/controller.ts | 2 +- .../src/Form/AutocompleteChoiceTypeExtension.php | 2 ++ src/Autocomplete/tests/Fixtures/Form/ProductType.php | 10 ++++++++++ .../tests/Functional/AutocompleteFormRenderingTest.php | 8 ++++++++ 4 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/Autocomplete/assets/src/controller.ts b/src/Autocomplete/assets/src/controller.ts index e140be2dcf2..3972ff08115 100644 --- a/src/Autocomplete/assets/src/controller.ts +++ b/src/Autocomplete/assets/src/controller.ts @@ -50,7 +50,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 cb0b98d4538..dc74396181f 100644 --- a/src/Autocomplete/src/Form/AutocompleteChoiceTypeExtension.php +++ b/src/Autocomplete/src/Form/AutocompleteChoiceTypeExtension.php @@ -47,6 +47,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 9261087fd8f..5e017c82eac 100644 --- a/src/Autocomplete/tests/Fixtures/Form/ProductType.php +++ b/src/Autocomplete/tests/Fixtures/Form/ProductType.php @@ -28,6 +28,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 f03e749537b..66abf664806 100644 --- a/src/Autocomplete/tests/Functional/AutocompleteFormRenderingTest.php +++ b/src/Autocomplete/tests/Functional/AutocompleteFormRenderingTest.php @@ -31,11 +31,19 @@ public function testFieldsRenderWithStimulusController() ->get('/test-form') ->assertElementAttributeContains('#product_category_autocomplete', 'data-controller', 'symfony--ux-autocomplete--autocomplete') ->assertElementAttributeContains('#product_category_autocomplete', 'data-symfony--ux-autocomplete--autocomplete-url-value', '/test/autocomplete/category_autocomplete_type') + ->assertElementAttributeContains('#product_category_autocomplete', 'required', 'required') ->assertElementAttributeContains('#product_portionSize', 'data-controller', 'symfony--ux-autocomplete--autocomplete') + ->assertElementAttributeContains('#product_portionSize', 'required', 'required') + + //not implemented in zenstruck/browser + //->elementAttributeNotExists('#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') ; + } public function testCategoryFieldSubmitsCorrectly()