|
| 1 | +<script lang="ts"> |
| 2 | + import { createForm } from "@cloudedots/svelte-forms"; |
| 3 | + import * as yup from "yup"; |
| 4 | + import UserAddressForm from "./UserAddressForm.svelte"; // Components |
| 5 | +
|
| 6 | + // (Optional) Form's Data type will be automatically inferred from "initialValues" in "createForm" if type of Data is not specified |
| 7 | + type FormData = { |
| 8 | + title: string, |
| 9 | + description: string, |
| 10 | + users: { |
| 11 | + name: string, |
| 12 | + email: string, |
| 13 | + address: { |
| 14 | + state: string, |
| 15 | + city: string, |
| 16 | + }, |
| 17 | + }[], |
| 18 | + }; |
| 19 | +
|
| 20 | + // Create Form Instance |
| 21 | + const { |
| 22 | + form, // Svelte Store<FormData> containing Form Data |
| 23 | + state, // Svelte Store<FormState> containing Form State - { [every_property]: { _touched: boolean, _errors: string[] }} |
| 24 | + isValid, // Svelte Store<boolean> containing entire Form's validation status |
| 25 | + isTouched, // Svelte Store<boolean> containing entire Form's touched status |
| 26 | + validateForm, // Function(highlight: 'none' | 'errors' | 'all' = 'none') for manually validting entire form |
| 27 | + handleChange, // Function(event: Event) to manually updating individual form control's state - can be used in place of "formControl" Action |
| 28 | + setTouched, // Function() for manually setting Form state as "touched" |
| 29 | + updateForm, // Function() for updating Form's Structure after Form Controls are Added or Removed in cases like Form Arrays |
| 30 | + formControl, // Svelte Action to be used with <input>, <select>, <textarea> or similar HTML input elements |
| 31 | + } = createForm<FormData>({ |
| 32 | + // Initial Values of Form |
| 33 | + initialValues: { |
| 34 | + title: "", // Simple String |
| 35 | + description: "", // Simple String |
| 36 | + users: [], // Complex Form Array |
| 37 | + }, |
| 38 | + // Yup Validation Schema |
| 39 | + validationSchema: yup.object().shape({ |
| 40 | + title: yup.string().min(8).required(), |
| 41 | + description: yup.string(), |
| 42 | + users: yup.array().of( |
| 43 | + yup.object().shape({ |
| 44 | + name: yup.string().required(), |
| 45 | + email: yup.string().email().required(), |
| 46 | + address: yup.object().shape({ |
| 47 | + state: yup.string().required(), |
| 48 | + city: yup.string(), |
| 49 | + }), |
| 50 | + }) |
| 51 | + ), |
| 52 | + }), |
| 53 | + validationClasses: { |
| 54 | + valid: "is-valid", // CSS class added to valid form controls |
| 55 | + invalid: "is-invalid", // CSS class added to invalid form controls |
| 56 | + showValid: true, // Add CSS classes to valid form controls |
| 57 | + showInvalid: true, // Add CSS classes to invalid form controls |
| 58 | + }, |
| 59 | + }); |
| 60 | +
|
| 61 | + // Add new user to Users Form Array |
| 62 | + const addUser = () => { |
| 63 | + // Update Form Data |
| 64 | + $form.users = [ |
| 65 | + ...$form.users, |
| 66 | + { |
| 67 | + name: "", |
| 68 | + email: "", |
| 69 | + address: { |
| 70 | + state: "", |
| 71 | + city: "", |
| 72 | + }, |
| 73 | + }, |
| 74 | + ]; |
| 75 | + updateForm(); // Manually trigger Form Update - Required |
| 76 | + }; |
| 77 | +
|
| 78 | + // Remove user from Users Form Array |
| 79 | + const removeUser = (index) => () => { |
| 80 | + $form.users = $form.users.filter((_, i) => i !== index); // Update Form Data |
| 81 | + $state.users = $state.users.filter((_, i) => i !== index); // Updating State is required after removing Form Controls |
| 82 | + updateForm(); // Manually trigger Form Update - Required |
| 83 | + }; |
| 84 | +
|
| 85 | + // Submit Form |
| 86 | + const onSubmit = () => { |
| 87 | + console.log($form); // Get Form Data |
| 88 | + }; |
| 89 | +
|
| 90 | + $: console.log($form, $state); // Log Form Data and Form State on every Change |
| 91 | +</script> |
| 92 | + |
| 93 | +<form on:submit|preventDefault={onSubmit}> |
| 94 | + <input |
| 95 | + placeholder="Title" |
| 96 | + name="title" |
| 97 | + bind:value={$form.title} |
| 98 | + use:formControl |
| 99 | + /> |
| 100 | + {#if $state.title._errors?.length} |
| 101 | + {#each $state.title._errors as error} |
| 102 | + <span class="error">{error}</span> |
| 103 | + {/each} |
| 104 | + {/if} |
| 105 | + |
| 106 | + <input |
| 107 | + placeholder="Description" |
| 108 | + name="description" |
| 109 | + bind:value={$form.description} |
| 110 | + use:formControl |
| 111 | + /> |
| 112 | + {#if $state.description._errors?.length} |
| 113 | + {#each $state.description._errors as error} |
| 114 | + <span class="error">{error}</span> |
| 115 | + {/each} |
| 116 | + {/if} |
| 117 | + |
| 118 | + {#each $form.users as user, index} |
| 119 | + <h2> |
| 120 | + User {user.name} |
| 121 | + <button type="button" on:click={removeUser(i)}> Remove User </button> |
| 122 | + </h2> |
| 123 | + |
| 124 | + <input |
| 125 | + placeholder="name" |
| 126 | + name="users[{index}].name" |
| 127 | + bind:value={user.name} |
| 128 | + use:formControl |
| 129 | + /> |
| 130 | + {#if $state.users[index].name._errors?.length} |
| 131 | + {#each $state.users[index].name._errors as error} |
| 132 | + <span class="error">{error}</span> |
| 133 | + {/each} |
| 134 | + {/if} |
| 135 | + |
| 136 | + <input |
| 137 | + placeholder="email" |
| 138 | + name="users[{index}].email" |
| 139 | + bind:value={user.email} |
| 140 | + use:formControl |
| 141 | + /> |
| 142 | + {#if $state.users[index].email._errors?.length} |
| 143 | + {#each $state.users[index].email._errors as error} |
| 144 | + <span class="error">{error}</span> |
| 145 | + {/each} |
| 146 | + {/if} |
| 147 | + |
| 148 | + <!-- Using with Components --> |
| 149 | + <UserAddressForm {form} {state} {formControl} {index} /> |
| 150 | + {/each} |
| 151 | + |
| 152 | + <button type="button" on:click={addUser}> Add User </button> |
| 153 | + |
| 154 | + <button type="button" on:click={() => validateForm("errors")}> |
| 155 | + Validate Form |
| 156 | + </button> |
| 157 | + |
| 158 | + <button type="submit" disabled={!$isValid}> Submit </button> |
| 159 | +</form> |
| 160 | + |
| 161 | +<style> |
| 162 | + .valid { |
| 163 | + border: 1px solid green; |
| 164 | + } |
| 165 | +
|
| 166 | + .invalid { |
| 167 | + border: 1px solid red; |
| 168 | + } |
| 169 | +
|
| 170 | + .error { |
| 171 | + color: red; |
| 172 | + } |
| 173 | +</style> |
0 commit comments