Example: register form
./schema.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import joi from 'joi';
// import { Input, Select, Textarea, CheckList, RadioList } from '@ferdotnet/reactive-form-builder';
// or, import here your custom inputs if needed
const schema = {
firstName: {
value: '',
label: 'First name',
placholder: 'Jonh',
input: Input,
type: 'text',
joi: joi.string().required().messages({
'string.empty': messages.REQUIRED
})
},
middleName: {
value: '',
label: 'Middle name',
placholder: 'J',
input: Input,
type: 'text',
joi: joi.string().empty('').optional()
},
lastName: {
value: '',
label: 'Last name',
placholder: 'Doe',
input: Input,
type: 'text',
joi: joi.string().required().messages({
'string.empty': messages.REQUIRED
})
},
email: {
value: '',
label: 'Email',
input: Input,
type: 'email',
joi: joi.string().regex(emailRegex).required().messages({
'string.pattern.base': messages.INVALID_EMAIL,
'string.empty': messages.REQUIRED
})
},
password: {
value: '',
label: 'Password',
input: Input,
type: 'password',
joi: joi.string().regex(passwordRegex).required().messages({
'string.pattern.base': messages.INVALID_PASSWORD,
'string.empty': messages.REQUIRED
})
},
passwordConfirmation: {
value: '',
label: 'Repeat password',
input: Input,
type: 'password',
joi: joi.any().equal(joi.ref('password')).required().messages({
'any.only': messages.PASSWORD_MUST_MATCH
})
}
}
export default schema;
./yourComponent.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import React from 'react';
import { useReactiveFormBuilder, ReactiveFormBuilder } from '@ferdotnet/reactive-form-builder';
import schema from './schema';
const MyComponent = () => {
const handler = useReactiveFormBuilder(schema, true); // Second param is to debug
const handleSubmit = (event: React.FormEvent) => {
event.preventDefault();
alert(JSON.stringify(handler.data));
};
return (
<form onSubmit={handleSubmit}>
<ReactiveFormBuilder handler={handler} />
<button type="submit" disabled={!handler.valid}>
Submit
</button>
</form>
);
};
export default MyComponent;