rob-balfre / svelte-select

Svelte Select. A select component for Svelte
https://svelte-select-examples.vercel.app
Other
1.26k stars 175 forks source link

required attribute dont work when isSearchable={false} #438

Closed Joslu99 closed 1 year ago

Joslu99 commented 1 year ago

Hi, I was developing a form using your Select component and I want to make it required using like https://github.com/rob-balfre/svelte-select/issues/214#issuecomment-746589030:

inputAttributes={ { required: !selectedRole } }

But I have found with to issues related to this. First of all, required is not working fine. If true, select doesn't check if value are set or not. I cannot submit form because always throw "Mandatory field". This is the reason why I use the negation of the value to control the required and set it to False when value is set.

The second problem is about "isSearchable" exported property. If I set it to false, because I don't want that the user use it to filter, required doesn't work. I mean, the previous solution found (or the usual required use) not work because select never detect that the component is required, so if required is true and Select is not searchable, I could submit the form without set any value on it.

Thanks!

rob-balfre commented 1 year ago

@Joslu99 You're just just setting required on the text input svelte-select uses internally to filter the list. If isSearchable is false then the internal input is disabled/removed so just do something like this instead...

https://svelte.dev/repl/d6ffe25a3d344e359e902ea0aecafc25?version=3.49.0