setValue
This function allows you to dynamically set the value of a registered field and have the options to validate and update the form state.
(name: string, value: any, options?: SetValueOptions) => void
Props
| Name | Type | Description |
|---|---|---|
| name | string | Form field name. |
| value | any | The value for the field. This argument is required and can not be undefined. |
| options.shouldValidate | boolean | This option will trigger validation for the target field. |
| options.shouldDirty | boolean | This option will update dirtyFields at the specified field level. |
| options.shouldTouch | boolean | This option will update touchedFields at the specified field level. |
import { createForm } from "solid-hook-form"
const ExampleForm = () => {
const { register, setValue } = createForm({
defaultValues: {
firstName: ""
}
})
return (
<form>
<input {...register("firstName", { required: true })} />
<button
onClick={() => {
setValue("firstName", "Bill")
}}
>
setValue
</button>
<button
onClick={() => {
setValue("firstName", "Luo", {
shouldValidate: true,
shouldDirty: true,
})
}}
>
setValue options
</button>
</form>
)
}