|
| 1 | +<script lang="ts"> |
| 2 | + import { superForm } from '$lib/client'; |
| 3 | + import SuperDebug from '$lib/client/SuperDebug.svelte'; |
| 4 | + import { formSchema, optionsSchema } from './schemas'; |
| 5 | +
|
| 6 | + export let data; |
| 7 | +
|
| 8 | + const { form, enhance, message, errors, allErrors, tainted } = superForm( |
| 9 | + data.form, |
| 10 | + { |
| 11 | + dataType: 'json', |
| 12 | + validators: formSchema, |
| 13 | + validationMethod: 'onblur', |
| 14 | + defaultValidator: 'clear' |
| 15 | + } |
| 16 | + ); |
| 17 | +
|
| 18 | + $: console.log($allErrors); |
| 19 | + $: options = optionsSchema._def.innerType.options; |
| 20 | +</script> |
| 21 | + |
| 22 | +<SuperDebug data={{ $form, $errors, $tainted }} /> |
| 23 | + |
| 24 | +{#if $message}<p>{$message}</p>{/if} |
| 25 | + |
| 26 | +<form method="POST" use:enhance> |
| 27 | + <div> |
| 28 | + <label for="multiselect">Multiselect</label> |
| 29 | + <select |
| 30 | + multiple |
| 31 | + name="multiselect" |
| 32 | + bind:value={$form.multiselect} |
| 33 | + data-invalid={$errors.multiselect} |
| 34 | + > |
| 35 | + {#each options as option} |
| 36 | + <option value={option}>{option}</option> |
| 37 | + {/each} |
| 38 | + </select> |
| 39 | + </div> |
| 40 | + <div> |
| 41 | + <label for="select">Select</label> |
| 42 | + <select |
| 43 | + name="select" |
| 44 | + bind:value={$form.select} |
| 45 | + data-invalid={$errors.select} |
| 46 | + > |
| 47 | + {#each options as option} |
| 48 | + <option value={option}>{option}</option> |
| 49 | + {/each} |
| 50 | + </select> |
| 51 | + </div> |
| 52 | + {#if $allErrors && $allErrors.length} |
| 53 | + <ul class="invalid"> |
| 54 | + {#each $allErrors as error}<li> |
| 55 | + {String(error.path)}: {error.message} |
| 56 | + </li>{/each} |
| 57 | + </ul> |
| 58 | + {/if} |
| 59 | + |
| 60 | + <button type="submit">Submit</button> |
| 61 | +</form> |
| 62 | + |
| 63 | +<style> |
| 64 | + label { |
| 65 | + display: block; |
| 66 | + margin-left: 5px; |
| 67 | + } |
| 68 | + input { |
| 69 | + margin: 5px; |
| 70 | + } |
| 71 | + .invalid { |
| 72 | + color: red; |
| 73 | + } |
| 74 | + button { |
| 75 | + background-color: black; |
| 76 | + color: white; |
| 77 | + padding: 5px; |
| 78 | + display: inline-block; |
| 79 | + margin: 5px; |
| 80 | + } |
| 81 | +</style> |
0 commit comments