Hooks
This library offers a way of hooking into the logic of the form fields. Below follows a list of all official hooks and their documentation. Please note, however, that you can also write your own hooks if needed.
Usage
All hooks are functions, that can take 0 or more arguments. They are returned within the hooks property of any form field, such as:
const formFields = {
name: {
label: 'Name',
hooks: [useAutoCapitalize(), useLength(2, 155), useRequired()]
}
}For more precise examples on how to use a hook, please refer to the code on examples page.
List of hooks
useAutoCapitalize
npm i @phormal/use-auto-capitalizeThis hook will automatically capitalize the first letter of each word in the input value. The hook is not configurable.
useEmail
npm i @phormal/use-emailThis hook will validate that the input is a valid email address. The hook is not configurable.
useLength
npm i @phormal/use-lengthThis hook can validate the minimum and maximum length on the input of a field.
| Argument | Type | Description |
|---|---|---|
minLength | number, undefined or null | Set the minimum length of the input |
maxLength | number, undefined or null | Set the maximum length of the input |
useRequired
npm i @phormal/use-requiredThis hook will validate that the input is not empty. The hook is not configurable.
useValidZip
npm i @phormal/use-valid-zipThis hook will validate that the format of a postal code matches the required format for a given country. The hook is not configurable, but requires 2 things to work properly:
- It can only be used on a field of type
text - It needs the presence of another field named
country. This field needs a value that matches a valid country code, according to the ISO-3166 Alpha-2 standard: https://www.iso.org/obp/ui/#search/code/ (opens in a new tab)