-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathRegisteredUserForm.js
More file actions
55 lines (44 loc) · 1.55 KB
/
RegisteredUserForm.js
File metadata and controls
55 lines (44 loc) · 1.55 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
51
52
53
54
import React, { Component } from 'react'
import {reduxForm, Field,reset} from 'redux-form'
import emailValidator from 'email-validator'
import ErrorField from '../common/ErrorField'
class RegisteredUserForm extends Component {
render() {
const {handleSubmit} = this.props
return (
<div>
<h2>Add user form</h2>
<form onSubmit={handleSubmit}>
<Field name="firstName" component={ErrorField}/>
<Field name="lastName" component={ErrorField} />
<Field name="email" component={ErrorField} />
<div>
<input type="submit" />
</div>
</form>
</div>
)
}
}
const validateName = (param,label,errors)=>{
if (!param) errors[label] = `${label} is required`
else if (/^[a-zA-Zа-яА-ЯёЁ]$/.test(param)) errors[label] = `invalid ${label}`
}
const validate = ({firstName,lastName, email}) => {
//debugger
let errors = {}
if (!email) errors.email = 'email is required'
else if (!emailValidator.validate(email)) errors.email = 'invalid email'
validateName(lastName,'lastName',errors)
validateName(firstName,'firstName',errors)
return errors
}
const afterSubmit = (result, dispatch) =>{
console.log('afterSubmit')
return dispatch(reset('RegisteredUserForm'));
}
export default reduxForm({
form: 'RegisteredUserForm',
onSubmitSuccess: afterSubmit,
validate
})(RegisteredUserForm)