Skip to content

Commit

Permalink
Only add the clear_button plugin to TomSelect if the form element is …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
bendavies committed Aug 11, 2022
1 parent 5893aca commit 9d852c7
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Autocomplete/assets/src/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: '' };
}

Expand Down
2 changes: 2 additions & 0 deletions src/Autocomplete/src/Form/AutocompleteChoiceTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
10 changes: 10 additions & 0 deletions src/Autocomplete/tests/Fixtures/Form/ProductType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 9d852c7

Please sign in to comment.