|
1 | | -# useFormHandler |
| 1 | +# useFormHandler: <font size=3>FormHandler</font> |
| 2 | + |
| 3 | +`useFormHandler` is our composable to handle forms, it takes one object as **optional** argument and returns various objects and methods that will allow us to handle our forms. |
| 4 | + |
| 5 | +## Props |
| 6 | + |
| 7 | +### initialValues: <font size=2>Record<string,any></font> |
| 8 | + |
| 9 | +::: warning |
| 10 | +Changing the initialValues will trigger a form reset, this means the formState and the values will return to its initial state. |
| 11 | +::: |
| 12 | + |
| 13 | +Values which we use to initialize the form, this is useful when we get an initial value state for the form from an external call. |
| 14 | + |
| 15 | +#### Example |
| 16 | +```vue |
| 17 | +<template> |
| 18 | + <input v-bind="register('firstName')"> // Should initially be 'John' |
| 19 | + <input v-bind="register('lastName')"> // Should initially be 'Doe' |
| 20 | + <pre>{{values}}</pre> // Should show the initialized values |
| 21 | +</template> |
| 22 | +<script setup lang="ts"> |
| 23 | + import { useFormHandler } from 'vue-form-handler' |
| 24 | +
|
| 25 | + const { register, values } = useFormHandler({ |
| 26 | + initialValues: { |
| 27 | + firstName: 'John', |
| 28 | + lastName: 'Doe' |
| 29 | + } |
| 30 | + }) |
| 31 | +</script> |
| 32 | +``` |
| 33 | + |
| 34 | +### interceptor: <font size=2>Interceptor</font> |
| 35 | + |
| 36 | +Function that allows us to intercept any value assignment, accept or deny it and perform operations before and after the assignment happens. |
| 37 | + |
| 38 | +#### Params |
| 39 | + |
| 40 | +The interceptor will be called passing an object as a parameter with the following: |
| 41 | + |
| 42 | +| attribute | type | description | |
| 43 | +|-----------|--------|--------------------------------------------| |
| 44 | +| name | string | Name of the field that is about to be set | |
| 45 | +| value | any | Value of the field that is about to be set | |
| 46 | +| clearError | ClearError | [API - clearError](/api/use-form-handler/clear-error) | |
| 47 | +| clearField | ClearField | [API - clearField](/api/use-form-handler/clear-field) | |
| 48 | +| formState | FormState | [API - formState](/api/use-form-handler/form-state) | |
| 49 | +| modifiedValues | ModifiedValues | [API - modifiedValues](/api/use-form-handler/modified-values) | |
| 50 | +| resetField | ResetField | [API - resetField](/api/use-form-handler/reset-field) | |
| 51 | +| resetForm | ResetForm | [API - resetForm](/api/use-form-handler/reset-form) | |
| 52 | +| setError | SetError | [API - setError](/api/use-form-handler/set-error) | |
| 53 | +| setValue | SetValue | [API - setValue](/api/use-form-handler/set-value) | |
| 54 | +| triggerValidation | TriggerValidation | [API - triggerValidation](/api/use-form-handler/trigger-validation) | |
| 55 | +| values | Record<string,any> | [API - values](/api/use-form-handler/values) | |
| 56 | + |
| 57 | +::: info |
| 58 | +As you can see, the interceptor is provided with everything the handler does provide but in a separate context. |
| 59 | +::: |
| 60 | + |
| 61 | +#### Expected return |
| 62 | + |
| 63 | +The interceptor expects a type of `boolean` or a `Promise<boolean>`, |
| 64 | +return true to proceed setting the value and false if the value should not be set. |
| 65 | + |
| 66 | +#### Example |
| 67 | + |
| 68 | +```vue |
| 69 | +<template> |
| 70 | + <select v-bind="register('continent')" placeholder="Choose your country"> |
| 71 | + <option disabled value=null>Choose your continent</option> |
| 72 | + <option value="AM">America</option> |
| 73 | + <option value="AS">Asia</option> |
| 74 | + <option value="EU">Europe</option> |
| 75 | + </select> |
| 76 | + <select v-bind="register('country')" placeholder="Choose your country"> |
| 77 | + <option disabled value=null>Choose your country</option> |
| 78 | + <option value="CAN">Canada</option> |
| 79 | + <option value="USA">United States</option> |
| 80 | + <option value="JAP">Japan</option> |
| 81 | + <option value="CHN">China</option> |
| 82 | + <option value="ESP">Spain</option> |
| 83 | + <option value="DEU">Germany</option> |
| 84 | + </select> |
| 85 | +</template> |
| 86 | +<script setup lang="ts"> |
| 87 | +import { useFormHandler } from 'vue-form-handler' |
| 88 | +
|
| 89 | +const interceptor = ({ name, clearField }) => { |
| 90 | + if (name === 'continent') { |
| 91 | + clearField('country') |
| 92 | + } |
| 93 | + return true |
| 94 | +} |
| 95 | +
|
| 96 | +const { register } = useFormHandler({ |
| 97 | + interceptor |
| 98 | +}) |
| 99 | +</script> |
| 100 | +``` |
| 101 | + |
| 102 | +### validate: <font size=2>FormValidation</font> |
| 103 | + |
| 104 | +Use this function in the case you'd rather perform a custom/different validation when submitting your form |
| 105 | + |
| 106 | +#### Example |
| 107 | + |
| 108 | +```vue |
| 109 | +<template @submit.prevent="handleSubmit(successFn)"> |
| 110 | + <form> |
| 111 | + <input v-bind="register('firstName')"> |
| 112 | + <input v-bind="register('lastName')"> |
| 113 | + <input type="number" v-bind="register('age')"> |
| 114 | + <input type="submit"> |
| 115 | + </form> |
| 116 | +</template> |
| 117 | +<script setup lang="ts"> |
| 118 | + import { useFormHandler } from 'vue-form-handler' |
| 119 | + |
| 120 | + const validation = (values) => values.age && Number(values.age)>=18 |
| 121 | + const successFn = (form:Record<string,any>) => { |
| 122 | + console.log({form}) |
| 123 | + } |
| 124 | +
|
| 125 | + const { register, handleSubmit } = useFormHandler({ |
| 126 | + validation |
| 127 | + }) |
| 128 | +</script> |
| 129 | +``` |
| 130 | + |
| 131 | +### validationMode: <font size="2">'onChange' | 'onBlur' | 'onSubmit' | 'always'</font> |
| 132 | + |
| 133 | +This option allows you to configure the validation mode or strategy the handler will follow. |
| 134 | + |
| 135 | +| name | type | description | |
| 136 | +|-----------|--------|--------------------------------------------| |
| 137 | +| onChange | string | Validation will trigger on the change event with each input, and lead to multiple re-renders. | |
| 138 | +| onBlur | string | Validation will trigger on the blur event. | |
| 139 | +| onSubmit | string | Validation will trigger on the submit event. | |
| 140 | +| always | string | Validation will trigger on change and blur events. | |
| 141 | + |
| 142 | +::: warning |
| 143 | +Using the `always` validationMode will have a more significant impact on performance. |
| 144 | +::: |
| 145 | + |
| 146 | +## Return |
| 147 | + |
| 148 | +- [clearError](/api/use-form-handler/clear-error) |
| 149 | +- [clearField](/api/use-form-handler/clear-field) |
| 150 | +- [formState](/api/use-form-handler/form-state) |
| 151 | +- [handleSubmit](/api/use-form-handler/handle-submit) |
| 152 | +- [modifiedValues](/api/use-form-handler/modified-values) |
| 153 | +- [register](/api/use-form-handler/register) |
| 154 | +- [resetField](/api/use-form-handler/reset-field) |
| 155 | +- [resetForm](/api/use-form-handler/reset-form) |
| 156 | +- [setError](/api/use-form-handler/set-error) |
| 157 | +- [setValue](/api/use-form-handler/set-value) |
| 158 | +- [triggerValidation](/api/use-form-handler/trigger-validation) |
| 159 | +- [unregister](/api/use-form-handler/unregister) |
| 160 | +- [values](/api/use-form-handler/values) |
0 commit comments