npm i formydable
import { useForm, useFormSupplier, FormSupplier, Validator, DoneTypingEvent } from "formydable"
This is a react form validator that is delightfully made by mytabowrks which is blazingly fast and can be easily use and implement in any field components with ease.
Rules validation order is from left to right. it will validate firstly the one on the left side which is the most priority. commonly required rule.
you can make controllable fields without a sweat. just add a single line and you are done.
you can make your own static rule messages by using "failedIn" without affecting the rules messages model.
Remember setting a schema only works once and will never change again after, because that is how react useState works. but if you want to update constantly the schema or the default value like when you are updating information, you can unmount the component(which you put the useForm) after submit then mount it again when you are about to update information...
form only exist to submit that is why it is not complete without submitting it on server side.
Remember setting a schema only works once and will never change again after, because that is how react useState works. but if you want to update constantly the schema or the default value like when you are updating information, you can unmount the component(which you put the useForm) after submit then mount it again when you are about to update information...
The fields which are not registered from the schema are excluded from FormEvent method json, param and paramArray.
If you want to involve the unregistered field. You can always use FormEvent method formData that will return FormData instance.
We use FormSupplier to provide formState, formUpdate and formRegistry to children components, and we use useFormSupplier to get FormSupplier provided to children components
It is inevitable that some form fields are multiple with the same name on it, the problem is we wanted to add those new or other fields into form state registry. for that reason formydable use alias registry and make a counter measure about those stuffs.
It is use to fire the event after user is done typing, that will save a lot of unessesary execution of state changes while typing.
developer`s perspective is different with user`s. that is why you can always use aliases (@) to make user`s apprehend the messages
you can make your own rules.
Registering customize rules is once and for all.
if you don`t like the default rules messages you can always do this to change it permanently.
API
Name | How to use | Description | Message |
---|---|---|---|
required | required | it will require the form field to be filled | The :attribute field is required |
it will validate if the field contain a valid e-mail | The :attribute field must be valid email | ||
min | min:<number> | it will validate the minumum character, number, checkbox is checked, select(multiple) is selected, file(multiple) is selected.e.g. min:10 | The :attribute field must be atleast :min (character, items, files) |
max | max:<number> | it will validate the maximum character, number, checkbox is checked, select(multiple) is selected, file(multiple) is selected.e.g. max:20 | The :attribute field may not be greater than :max (character, items, files) |
mimes | mimes:<files mimes> | it will validate the specific mimes of the files which are allowed.e.g. mimes:jpg,pdf,rar | The :attribute only allows :mimes |
alpha | alpha | it will validate if the field value is only contain letter | The :attribute may only contain letters |
alpha_space | alpha_space | it will validate if the field only contain letters with spaces | The :attribute must contain alphabet with spaces |
alpha_num | alpha_num | it will validate if the field contain letters with numbers | The :attribute may only contain letters and numbers. |
alpha_slug | alpha_slug | it will validate if the field contain letters, numbers, and underscore | The :attribute may only contain letters, numbers, and underscore. |
alpha_dash | alpha_dash | it will validate if the field contain letters with numbers and dashes | The :attribute may only contain letters, numbers, and dashes. |
url | url | it will validate if the field contain valid url | The :attribute must be a valid url. |
max_size | max_size:<number> | it will validate if the field contain a maximum file size and the size must calculate in kilobytes.e.g. max_size:5000 | The :attribute may not be greater :max_size kilobytes. |
min_size | min_size:<number> | it will validate if the field contain a minimum file size and the size must calculate in kilobytes.e.g. min_size:1000 | The :attribute must be atleast :min_size kilobytes. |
required_if | required_if:<field_id>=<expected_value> | it will require the field, if the target field matches the expected value. you can use exact value or regular expression likerequired_if:bio=.+. .+ means has any value.e.g. required_if:country=AU since most of the time field ids are not the same as the labels and same with the values label. that is why you can use Aliasing(@)e.g. required_if:country@Country=AU@Australia | The :attribute field is required when :required_if is :value. |
same | same:<field_id> | it will validate the field until the target field contain the same value.e.g. same:pass since most of the time field ids are not the same as the labels. you can use Aliasing(@)e.g. same:pass@Password | The :attribute and :same must match. |
it is use to register schema and to get formState, formUpdate, formSubmit, formRegistry.
Property Property | Type Type | Default Default | Description Description |
---|---|---|---|
const { | |||
formState, | function | - | it is use to fetch the field state. |
formUpdate, | function | - | it is use to update the field state. and it is commonly used directly in onChange event or if more complex you can use it inside the EventHandler. |
formRegistry, | function | - | it is use to remotely register the field rules into the schema. |
formSubmit | function | - | it is use to handle the submission of form field. |
} = useForm( | |||
schema? | object | - | it is the pre-registration of the fields. |
) |
it is use to set the useForm functions into useFormSupplier which you can use under FormSupplier as a wrapper.
Property Property | Type Type | Default Default | Description Description |
---|---|---|---|
value | object | - | it must contain the useForm funtions: formState, formUpdate, and formRegistry which will be supply to all of its descendant using useFormSupplier. |
it is use to fetch FormSupplier provided.
Property Property | Type Type | Default Default | Description Description |
---|---|---|---|
const { | |||
formState, | function | - | it is use to fetch the field state. |
formUpdate, | function | - | it is use to update the field state. and it is commonly used directly in onChange event or if more complex you can use it inside the EventHandler. |
formRegistry, | function | - | it is use to remotely register the field rules into the schema. |
} = useFormSupplier() |
it is use to fire the event after user done typing, that will save a lot of unessesary execution while typing.
Property Property | Type Type | Default Default | Description Description |
---|---|---|---|
DoneTypingEvent( | |||
callback, | function | - | it is the callback function when the user stop typing. |
delay | number | 700 | it is the miliseconds aloted for users typing, if its exceeded on aloted time then it will know that the user stops typing. |
) |
Validator
it is use to make your own rules.
Property Property | Type Type | Default Default | Description Description |
---|---|---|---|
Validator.rulesExtend({ | |||
[ruleName] | object | - | your rule name. |
}) |
it is use to change the message of the existing rules.
Property Property | Type Type | Default Default | Description Description |
---|---|---|---|
Validator.rulesUpdateMessage({ | |||
ruleName, | string | - | the name of the rule you want to update the message |
message | string | - | it is the message template will be displayed when your rules is invalid. |
}) |