-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathNewPersonForm.js
More file actions
48 lines (39 loc) · 1.35 KB
/
NewPersonForm.js
File metadata and controls
48 lines (39 loc) · 1.35 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
import React, { Component } from 'react'
import {reduxForm, Field} from 'redux-form'
import validateEmail from 'email-validator'
import ErrorField from '../common/ErrorField'
class NewPersonForm extends Component {
static propTypes = {
};
componentWillReceiveProps = (nextProps) => {
const {submitSucceeded, reset} = this.props;
if (nextProps.submitSucceeded === true && nextProps.submitSucceeded !== submitSucceeded) {
reset('person');
}
}
render() {
return (
<div>
<form onSubmit={this.props.handleSubmit}>
<Field name="firstName" label="first name" component={ErrorField}/>
<Field name="lastName" label="last name" component={ErrorField}/>
<Field name="email" label="email" component={ErrorField}/>
<div>
<input type="submit" />
</div>
</form>
</div>
)
}
}
function validate({firstName, email}) {
const errors = {}
if (!firstName) errors.firstName = 'first name is required'
if (!email) errors.email = 'email is required'
else if (!validateEmail.validate(email)) errors.email = 'email is invalid'
return errors
}
export default reduxForm({
form: 'person',
validate
})(NewPersonForm)