register

This method allows you to register an input or select element and apply validation rules to Solid Hook Form. Validation rules are all based on the HTML standard and also allow for custom validation methods.

(name: string, options?: Rules) => ({ ref, name, onInput, onChange, onBlur })

Props


NameTypeDescription
namestringInput's name.
optionsRulesInput's behavior.
const { register } = createForm({ defaultValues: { firstName: "", lastName: "" } }) return ( <form> <input {...register("firstName", { required: true })} /> <input {...register("lastName", { minLength: 5 })} /> </form> )

Returns


NameTypeDescription
namestringInput's name being registered.
refFunctionElement ref used to connect form to the input.
onInputFunctiononInput prop to subscribe the input change
onChangeFunctiononChange prop to subscribe the input change
onBlurFunctiononBlur prop to subscribe the input blur

It is possible to use nested form values:

const { register } = createForm({ defaultValues: { user: { profile: { firstName: "" } } } }) return ( <form> <input {...register("user.profile.firstName")} /> </form> )

Rules

Options to describe registered input's behavior.

NameTypeDescription
requiredbooleanIndicates that the input must have a value before the form can be submitted.
maxLengthnumberThe maximum length of the value to accept for this input.
minLengthnumberThe minimum length of the value to accept for this input.
maxnumberThe maximum value to accept for this input.
minnumberThe minimum value to accept for this input.
patternRegExpThe regex pattern for the input.
validateFunctionValidate function will be executed on its own without depending on other validation rules included in the required attribute.
valueAsNumberbooleanReturns Number normally. If something goes wrong NaN will be returned.

Provide both validation rule and error message:

const { register } = createForm({ defaultValues: { firstName: "", lastName: "" } }) return ( <form> <input {...register("firstName", { required: "First name is required" })} /> <input {...register("lastName", { minLength: { value: 5, message: "Min 5 characters" } })} /> </form> )