svelte-forms icon indicating copy to clipboard operation
svelte-forms copied to clipboard

Missing docs: validating checkboxes

Open eden-omb opened this issue 3 years ago • 4 comments

How does validation work with checkbox inputs?

eden-omb avatar Jan 29 '22 23:01 eden-omb

Perhaps a new label is needed for docs issues? It's more bug than feature request, but it's not very informative.

eden-omb avatar Jan 29 '22 23:01 eden-omb

Hi @eden-omb ,

What do you mean?

I had no trouble validating checkboxes following the svelte-documentation and svelte-forms field value.


<script>
import { field } from 'svelte-forms';

const box = field('booleanValue', false);
</script>

<input type"checkbox" bind:checked={$box.value}

chainlist avatar Feb 06 '22 01:02 chainlist

That's just for one checkbox. Svelte docs give us (bind:group)[https://svelte.dev/docs#template-syntax-element-directives-bind-group] to collect values like that into an array. Radio buttons also use bind:group but uses it to set one value like a normal input. Since multiple checkboxes requires an array value and therefore validating an array value, this needs documentation. How can I, for example, validate every selected value, or selecting a minimum amount of values, or something like all selected options having at least some sum? This would also be relevant for <select multiple>.

eden-omb avatar Feb 06 '22 09:02 eden-omb

bind:group is working fine with svelte-forms. Here is a working example:

<script>
  import { form as formValidation, field } from 'svelte-forms'

  const delivery_option = field('delivery_option', '', [])
  const formValidator = formValidation(delivery_option)
</script>

<form>
  <div>
    <input bind:group={$delivery_option.value} type="radio" name="delivery_option" value="Pick up">
    <label>Pick up</label>
  </div>
  
  <div>
    <input bind:group={$delivery_option.value} type="radio" name="delivery_option" value="USPS">
    <label>USPS</label>
  </div>
</form>

<div>Selected delivery method: {$delivery_option.value}</div>

I plan on contributing with the documentation, probably during weekend.

lnfel avatar Mar 09 '23 06:03 lnfel