Skip to content

[Autocomplete] required choice types end up not being required. #402

@bendavies

Description

Hi there,

There seems to be an issue with how tom-select is configured, for required, non-multiple choice types.

Given a form type configuration like

$builder->add('isAttending', ChoiceType::class, [
    'choices' => [
        'Yes' => 'yes',
        'No' => 'no',
        'Maybe' => 'maybe',
    ],
]);

You will be left with a select box like:
image

All well and good, an option is mandatory.

However, convert to autocomplete and you're left with:
image

see the issue? It's clearable because the clear plugin is added, so you can be left with no value.

image

this shouldn't be possible, as it isn't with the simple select box above.

We can try fix fix:

$builder->add('isAttending', ChoiceType::class, [
    'choices' => [
        'Yes' => 'yes',
        'No' => 'no',
        'Maybe' => 'maybe',
    ],
    'autocomplete' => true,
    'required' => true,
]);

this has no effect for 2 reasons:

  1. the symfony/ux-autocomplete controller still adds the clear plugin (while the easyadmin tom-select controller takes requried into considertion, from which symfony/ux-autocomplete was inspired by)
  2. the easy admin version wouldn't work either, because symfony removes the required attribute for select boxes when it's impossible not to submit a value.

Number 2 can be "fixed"/overridden like this:

$builder->add('isAttending', ChoiceType::class, [
    'choices' => [
        'Yes' => 'yes',
        'No' => 'no',
        'Maybe' => 'maybe',
    ],
    'attr' => [
        'required' => true,
    ],
    'autocomplete' => true,
]);

but that would mean we still have to fix the stimulus controller here to not add the clear button if there is a required attribute.

my "fix" above (adding 'attr' => ['required' => true]) is probably too onerous to put on the end user. Maybe the autocomplete form type should do this automatically?

Thanks

Metadata

Assignees

No one assigned

    Labels

    BugBug Fix

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions