From 43db9630a42c5d1dc0e1b41c45bfa0c02a53803c Mon Sep 17 00:00:00 2001 From: Nicco Date: Sat, 19 Oct 2019 20:15:18 +0200 Subject: [PATCH] Update README.md --- README.md | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 83ddd9b..83a9a1c 100644 --- a/README.md +++ b/README.md @@ -188,7 +188,9 @@ const initial = { ### Validators -A validator is an object that taked in either a `RegExp` or a `Function` (can be async). Optionally you can pass a message string that will be displayed instead of the default one. +A validator is an object that taked in either a `RegExp` or a `Function` (can be async) or an array of those. Optionally you can pass a message string that will be displayed instead of the default one. + +A validator functions takes the current value as input and should return a `boolean` or a `string`. If returned `true` the input counts as valid, if `false` it's not. If you pass a string formhero will treat it as not valid and display the string returned as error message. ###### Example: Regular Expression @@ -238,6 +240,17 @@ const validators = { } ``` +###### Example: Dynamic Error Message + +```javascript +const validators = { + username: async (s: string) => { + const taken = await API.isUsernameTaken(s) + return taken ? 'Username is taken': true + } +} +``` + ### Options Sometimes it's practical to have some different default values when using for example react-native or some other framework where the default `value`, `onChange` and `(e)=> e.target.value` do not apply.