ux icon indicating copy to clipboard operation
ux copied to clipboard

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

Open bendavies opened this issue 2 years ago • 8 comments

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

bendavies avatar Jul 26 '22 16:07 bendavies

Duplicate of #379 and fixed with #401

mario-fehr avatar Jul 27 '22 03:07 mario-fehr

I do not agree that those issues are the same as this one - those are about completely overriding the default plugins setup by symfony/ux-autocomplete.

this issue: I think symfony/ux-autocomplete should be more intelligent/correct out of the box with reguards to required select elements.

bendavies avatar Jul 27 '22 09:07 bendavies

@bendavies Agree, not the same but related. I think best way is to only add the clear button when the select is not required. Another option is that we never set plugins automaticly but I think that gives bad DX

maartendekeizer avatar Aug 02 '22 14:08 maartendekeizer

Hi there!

Really excellent issue and explanation of the problems Ben - thanks for that. As Ben mentioned, the required "form view variable" is not always printed out as a required attribute on the select element, which is why the current solution of looking for that attribute in JavaScript doesn't work. Could we, instead, read the required variable ($view->vars['required']) in the form type extension - https://github.com/symfony/ux/blob/8e84d49a92b70f99001b8817cd3129a6215175f6/src/Autocomplete/src/Form/AutocompleteChoiceTypeExtension.php#L42 - and then use it to pass in a new "value" to the Stimulus controller?

weaverryan avatar Aug 03 '22 14:08 weaverryan

yep, that looks doable. we just need to pass a new required attr which will make it into the template

bendavies avatar Aug 04 '22 10:08 bendavies

@bendavies do you have any news? :eyes:

LucileDT avatar Jun 19 '23 14:06 LucileDT

forgot about this. i'll take a look again

bendavies avatar Jun 19 '23 21:06 bendavies