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()