Update README.md

This commit is contained in:
Nicco 2019-10-19 20:15:18 +02:00 committed by GitHub
parent 1e2ad56d6a
commit 43db9630a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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.