-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathUserForm.js
More file actions
50 lines (42 loc) · 1.42 KB
/
UserForm.js
File metadata and controls
50 lines (42 loc) · 1.42 KB
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
import React, {Component} from 'react'
import {reduxForm, Field} from 'redux-form'
import validator from 'email-validator'
import ErrorField from '../common/ErrorField'
class UserForm extends Component {
render() {
return (
<form onSubmit={this.props.handleSubmit}>
<div>
<label>First name:
<Field name = "firstName" component = {ErrorField} />
</label>
</div>
<div>
<label>Last name:
<Field name = "lastName" component = {ErrorField} />
</label>
</div>
<div>
<label>Email:
<Field name = "email" component = {ErrorField} />
</label>
</div>
<div>
<button type = "submit">add user</button>
</div>
</form>
)
}
}
const validate = ({ firstName, lastName, email }) => {
const errors = {}
if (!email) errors.email = 'email is a required field'
if (email && !validator.validate(email)) errors.email = 'incorrect email format'
if (!firstName) errors.firstName = 'First name is a required field'
if (!lastName) errors.lastName = 'Last name is a required field'
return errors
}
export default reduxForm({
form : "users",
validate
})(UserForm)