-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathNewUserForm.js
More file actions
50 lines (41 loc) · 1.43 KB
/
NewUserForm.js
File metadata and controls
50 lines (41 loc) · 1.43 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 PropTypes from 'prop-types';
import {reduxForm, Field} from 'redux-form';
import validator from 'email-validator';
import ErrorField from '../common/ErrorField';
class NewUserForm extends Component {
static propTypes = {
handleSubmit: PropTypes.func.isRequired
};
render() {
return (
<div>
<h3>Add user</h3>
<form onSubmit={this.props.handleSubmit}>
<div>
First name: <Field name='firstName' component={ErrorField} />
</div>
<div>
Last name: <Field name='lastName' component={ErrorField} />
</div>
<div>
Email: <Field name='email' component={ErrorField} type='email' />
</div>
<input type='submit' />
</form>
</div>
)
}
}
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: 'new-user',
validate
})(NewUserForm)