Skip to content
This repository was archived by the owner on Sep 2, 2023. It is now read-only.

Commit 8261d27

Browse files
add validation to profile page
1 parent 2897837 commit 8261d27

File tree

2 files changed

+108
-15
lines changed

2 files changed

+108
-15
lines changed

src/settings/SettingsAddress.js

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import React, { PureComponent } from 'react';
2+
import head from 'lodash.head';
3+
import validate from 'validate.js';
24
import { Legend, Button, InputGroup, ErrorMsg, ServerErrorMsg } from 'binary-components';
35
import { actions } from '../_store';
46
import States from './States';
57
import UpdateNotice from '../containers/UpdateNotice';
68
import * as LiveData from '../_data/LiveData';
9+
import { getConstraints } from './SettingsAddress.validation.config';
710

811
export default class SettingsAddress extends PureComponent {
912

@@ -28,6 +31,14 @@ export default class SettingsAddress extends PureComponent {
2831
address_state: props.address_state,
2932
address_postcode: props.address_postcode,
3033
phone: props.phone,
34+
touched: {
35+
address_line_1: false,
36+
address_line_2: false,
37+
address_city: false,
38+
address_state: false,
39+
address_postcode: false,
40+
phone: false,
41+
},
3142
};
3243
}
3344

@@ -36,15 +47,27 @@ export default class SettingsAddress extends PureComponent {
3647
actions.getStatesForCountry(country_code);
3748
}
3849

39-
onEntryChange = (e: SyntheticEvent) =>
40-
this.setState({ [e.target.id]: e.target.value });
50+
onEntryChange = (e: SyntheticEvent) =>
51+
this.setState({
52+
[e.target.id]: e.target.value,
53+
touched: { ...this.state.touched, [e.target.id]: true },
54+
hasError: false,
55+
}, () => {
56+
this.validateForm();
57+
});
4158

42-
onFormSubmit = (e: SyntheticEvent) => {
43-
e.preventDefault();
59+
validateForm = () => {
60+
this.constraints = getConstraints(this.props, this.state);
4461
this.setState({
45-
validatedOnce: true,
62+
errors: validate(this.state, this.constraints, { format: 'grouped', fullMessages: false, cleanAttributes: false }) || {},
4663
});
47-
if (this.allValid) {
64+
}
65+
66+
onFormSubmit = (e: SyntheticEvent) => {
67+
e.preventDefault();
68+
if (Object.keys(this.state.errors).length > 0) {
69+
this.setState({ hasError: true });
70+
} else {
4871
this.performUpdateSettings();
4972
}
5073
}
@@ -71,63 +94,69 @@ export default class SettingsAddress extends PureComponent {
7194

7295
render() {
7396
const { states } = this.props;
74-
const { validatedOnce, address_line_1, address_line_2, address_city, address_state,
75-
address_postcode, country_code, phone, phoneError, serverError, success } = this.state;
76-
77-
this.allValid = address_line_1 && address_city;
97+
const { address_line_1, address_line_2, address_city, address_state,
98+
address_postcode, country_code, phone, serverError, success, hasError, touched, errors } = this.state;
7899

79100
return (
80101
<form className="settings-address" onSubmit={this.onFormSubmit}>
81102
{serverError && <ServerErrorMsg text={serverError} />}
103+
{hasError && <ErrorMsg text="Please fill the form with valid values" />}
82104
<UpdateNotice text="Address updated" show={success} />
83105
<Legend text="Address" />
84106
<InputGroup
85107
id="address_line_1"
108+
name="address_line_1"
86109
type="text"
87110
label="Address"
88111
value={address_line_1}
89112
onChange={this.onEntryChange}
90113
/>
91-
{validatedOnce && !address_line_1 &&
92-
<ErrorMsg text="This field is required." />
93-
}
114+
{touched.address_line_1 && <ErrorMsg text={head((errors || {}).address_line_1)} />}
94115
<InputGroup
95116
id="address_line_2"
117+
name="address_line_2"
96118
type="text"
97119
label=" "
98120
value={address_line_2}
99121
onChange={this.onEntryChange}
100122
/>
123+
{touched.address_line_2 && <ErrorMsg text={head((errors || {}).address_line_2)} />}
101124
<InputGroup
102125
id="address_city"
126+
name="address_city"
103127
type="text"
104128
label="Town/City"
105129
defaultValue={address_city}
106130
onChange={this.onEntryChange}
107131
/>
132+
{touched.address_city && <ErrorMsg text={head((errors || {}).address_city)} />}
108133
<States
109134
id="address_state"
135+
name="address_state"
110136
country={country_code}
111137
states={states}
112138
onChange={this.onEntryChange}
113139
selected={address_state}
114140
/>
141+
{touched.address_state && <ErrorMsg text={head((errors || {}).address_state)} />}
115142
<InputGroup
116143
id="address_postcode"
144+
name="address_postcode"
117145
type="text"
118146
label="Postal Code / ZIP"
119147
defaultValue={address_postcode}
120148
onChange={this.onEntryChange}
121149
/>
150+
{touched.address_postcode && <ErrorMsg text={head((errors || {}).address_postcode)} />}
122151
<InputGroup
123152
id="phone"
153+
name="phone"
124154
type="tel"
125155
label="Telephone"
126156
defaultValue={phone}
127157
onChange={this.onEntryChange}
128158
/>
129-
{phoneError === 'length' && <ErrorMsg text="You should enter between 6-35 characters." />}
130-
{phoneError === 'allowed' && <ErrorMsg text="Only numbers, space, - are allowed." />}
159+
{touched.phone && <ErrorMsg text={head((errors || {}).phone)} />}
131160
<Button
132161
text="Update"
133162
onClick={this.tryUpdate}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import validate from 'validate.js';
2+
3+
/*eslint-disable */
4+
validate.validators.validateAddress = (value) => {
5+
if(/[`~!#$%^&*)(_=+\[}{\]\\";:\?><|]+/.test(value)) {
6+
return 'Only letters, numbers, space, hyphen, period, and apostrophe are allowed.';
7+
}
8+
}
9+
10+
validate.validators.validateGeneral = (value) => {
11+
if(/[`~!@#$%^&*)(_=+\[}{\]\\\/";:\?><,|\d]+/.test(value)) {
12+
return 'Only letters, space, hyphen, period, and apostrophe are allowed.';
13+
}
14+
}
15+
/*eslint-enable */
16+
17+
export const getConstraints = () => {
18+
const constraints = {
19+
address_line_1: {
20+
presence: {
21+
presence: true,
22+
message: 'This field is required.',
23+
},
24+
validateAddress: true,
25+
},
26+
address_line_2: {
27+
validateAddress: true,
28+
},
29+
address_city: {
30+
presence: {
31+
presence: true,
32+
message: 'This field is required.',
33+
},
34+
validateGeneral: true,
35+
},
36+
address_state: {
37+
presence: {
38+
presence: true,
39+
message: 'This field is required.',
40+
},
41+
},
42+
address_postcode: {
43+
format: {
44+
/*eslint-disable */
45+
pattern: /^([a-zA-Z\d-\s])*$/,
46+
/*eslint-enable */
47+
message: 'Only letters, numbers, space, and hyphen are allowed.',
48+
},
49+
},
50+
phone: {
51+
presence: {
52+
presence: true,
53+
message: 'This field is required.',
54+
},
55+
format: {
56+
/*eslint-disable */
57+
pattern: /^\+?[0-9\s]*$/,
58+
/*eslint-enable */
59+
message: 'Only numbers and spaces are allowed.',
60+
},
61+
},
62+
};
63+
return constraints;
64+
};

0 commit comments

Comments
 (0)