diff --git a/src/Autocomplete/assets/dist/controller.js b/src/Autocomplete/assets/dist/controller.js index bc12f0d3e50..779fc7d9b23 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 fc93c44c1ea..65ffe7637c6 100644 --- a/src/Autocomplete/assets/src/controller.ts +++ b/src/Autocomplete/assets/src/controller.ts @@ -92,7 +92,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 edcda6866c2..be6ab919212 100644 --- a/src/Autocomplete/src/Form/AutocompleteChoiceTypeExtension.php +++ b/src/Autocomplete/src/Form/AutocompleteChoiceTypeExtension.php @@ -49,6 +49,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 ce99325d058..f5efa1b63f8 100644 --- a/src/Autocomplete/tests/Fixtures/Form/ProductType.php +++ b/src/Autocomplete/tests/Fixtures/Form/ProductType.php @@ -29,6 +29,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 0e8c2daa5f9..57de0704c67 100644 --- a/src/Autocomplete/tests/Functional/AutocompleteFormRenderingTest.php +++ b/src/Autocomplete/tests/Functional/AutocompleteFormRenderingTest.php @@ -34,10 +34,19 @@ public function testFieldsRenderWithStimulusController() ->assertElementAttributeContains('#product_category_autocomplete', 'data-symfony--ux-autocomplete--autocomplete-url-value', '/test/autocomplete/category_autocomplete_type') ->assertElementAttributeContains('#product_category_autocomplete', 'data-symfony--ux-autocomplete--autocomplete-min-characters-value', '2') ->assertElementAttributeContains('#product_category_autocomplete', 'data-symfony--ux-autocomplete--autocomplete-max-results-value', '25') + ->assertElementAttributeContains('#product_category_autocomplete', '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') ; }