EasyAdminBundle icon indicating copy to clipboard operation
EasyAdminBundle copied to clipboard

Disallow clear in AssociationField

Open itomxi opened this issue 3 years ago • 6 comments

Short description of what this feature will allow to do: There is no reason to show clear buttons f.e. for OneToMany field where the other side is not nullable.

Example of how to use this feature

AssociationField::new('marks')->setFormTypeOptions([ 'attr.data-ea-allow-clear' => 'false' ])

itomxi avatar Apr 05 '22 19:04 itomxi

Hello @itomxi,

Use the following:

AssociationField::new('marks')->setFormTypeOption('attr', ['data-ea-allow-clear' => 'false']);

Kind regards

fabienlem avatar Apr 07 '22 18:04 fabienlem

Hi @fabienlem, Thank you for your idea, unfortunately id doesn't work. I can see data-ea-allow-clear="false" using inspector on the select. But clear buttons are still there.

I was wondering, where did you find this option? There is only one mention of allow-clear in the whole codebase, and not in this context (I am using version 4.0.4).

itomxi avatar Apr 10 '22 17:04 itomxi

My bad! I thought data-ea-allow-clear (from your original post) was an existing option and you didn't know how to pass it to the view.

I didn't see such feature in the javascript plugin documentation: https://tom-select.js.org/

fabienlem avatar Apr 11 '22 18:04 fabienlem

Hi, tom-select doesn't show clear button in items by default. It must be enabled via plugin remove_button. It looks like it is enabled in autocomplete.js using

if (null !== element.getAttribute('multiple')) { config.plugins.remove_button = { title: '' }; }

This approach is wrong, since it doesn't look whether the relation can or cannot be nullable.

Any ideas what to do? I would suggest adding the mentioned data-ea-allow-clear and altering autocomplete.js to look for it.

itomxi avatar Apr 12 '22 09:04 itomxi

I've the same problem. When setting an AssociationField as required (->setRequired(true)), it does not set the required attribute on the select element.

The comparison in assets/js/autocomplete.js on line 36 is thus for this occasions pointless:

if (null === element.getAttribute('required') && null === element.getAttribute('disabled')) {
   config.plugins.clear_button = { title: '' };
}

Jamie-Vandenbalck avatar Jun 09 '22 15:06 Jamie-Vandenbalck