asyncRule

Specification

Asynchronous rule of the field applied as the top priority validation after its sibling Field.props.rule resolves. Executed on field blur.
  • asyncRule is designed for on blur asynchronous validation of the fields.
  • asyncRule is designed for precise unique field validation.
  • asyncRule must always return the specified AsyncRulePayload Object.
  • asyncRule will not be called if it has been previously resolved and the value of the field has not changed.

Definition

1
type AsyncRule = (params) => AsyncRulePayload
Copied!
1
type AsyncRulePayload = {
2
valid: boolean,
3
extra?: {
4
[exraKey: string]: any
5
}
6
}
Copied!

Parameters

Parameter name
Type
Description
value
any
The current value of the field.
fieldProps
Object
Props of the current field.
fields
Object
Map of all fields.
form
Object
A reference to the current Form

Example

1
import React from 'react'
2
import { Form } from 'react-advanced-form'
3
import { Input } from 'react-advanced-form-addons'
4
5
export default class Example extends React.Component {
6
validateUsername = ({ value, fieldProps, fields, form }) => {
7
return fetch('https://check.if/user/exists', {
8
method: 'POST',
9
body: JSON.stringify(value),
10
})
11
.then(res => res.json())
12
.then((res) => {
13
const { statusCode } = res
14
15
return {
16
valid: (statusCode === 'SUCCESS')
17
}
18
})
19
}
20
21
render() {
22
return (
23
<Form>
24
<Input
25
name="username"
26
asyncRule={this.validateUsername} />
27
</Form>
28
);
29
}
30
}
Copied!
Last modified 3yr ago