summaryrefslogtreecommitdiffstats
path: root/src/generic-components/input/validation/ValidationInput.jsx
blob: 7c3c2a82b4ab80cb3819f22a829c791fad1399da (plain)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 * Copyright © 2017 Amdocs
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 *
 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 */
/**
	* Used for inputs on a validation form.
	* All properties will be passed on to the input element.
	*
	* The following properties can be set for OOB validations and callbacks:
	- required: Boolean:  Should be set to true if the input must have a value
	- numeric: Boolean : Should be set to true id the input should be an integer
	- onChange : Function :  Will be called to validate the value if the default validations are not sufficient, should return a boolean value
	indicating whether the value is valid
	- didUpdateCallback :Function: Will be called after the state has been updated and the component has rerendered. This can be used if
	there are dependencies between inputs in a form.
	*
	* The following properties of the state can be set to determine
	* the state of the input from outside components:
	- isValid : Boolean - whether the value is valid
	- value : value for the input field,
	- disabled : Boolean,
	- required : Boolean - whether the input value must be filled out.
	*/
import React from 'react';
import ReactDOM from 'react-dom';
import Validator from 'validator';
import Input from 'react-bootstrap/lib/Input.js';
import Overlay from 'react-bootstrap/lib/Overlay.js';
import Tooltip from 'react-bootstrap/lib/Tooltip.js';
import isEqual from 'lodash/lang/isEqual.js';
import i18n from 'utils/i18n/i18n.js';

import InputOptions  from '../inputOptions/InputOptions.jsx';

const globalValidationFunctions = {
		required: value => value !== '',
		maxLength: (value, length) => Validator.isLength(value, {max: length}),
		minLength: (value, length) => Validator.isLength(value, {min: length}),
		pattern: (value, pattern) => Validator.matches(value, pattern),
		numeric: value => Validator.isNumeric(value),
		maxValue: (value, maxValue) => value < maxValue,
		alphanumeric: value => Validator.isAlphanumeric(value),
		alphanumericWithSpaces: value => Validator.isAlphanumeric(
				value.replace(/ /g, '')),
		validateName: value => Validator.isAlphanumeric(
				value.replace(/\s|\.|\_|\-/g, ''), 'en-US'),
		validateVendorName: value => Validator.isAlphanumeric(
				value.replace(/[\x7F-\xFF]|\s/g, ''), 'en-US'),
		freeEnglishText: value => Validator.isAlphanumeric(
				value.replace(/\s|\.|\_|\-|\,|\(|\)|\?/g, ''), 'en-US'),
		email: value => Validator.isEmail(value),
		ip: value => Validator.isIP(value),
		url: value => Validator.isURL(value)
};

const globalValidationMessagingFunctions = {
		required: () => i18n('Field is required'),
		maxLength: (value, maxLength) => i18n(
				'Field value has exceeded it\'s limit, {maxLength}. current length: {length}',
				{
						length: value.length,
						maxLength
				}),
		minLength: (value, minLength) => i18n(
				'Field value should contain at least {minLength} characters.', {minLength}),
		pattern: (value, pattern) => i18n(
				'Field value should match the pattern: {pattern}.', {pattern}),
		numeric: () => i18n('Field value should contain numbers only.'),
		maxValue: (value, maxValue) => i18n(
				'Field value should be less then: {maxValue}.', {maxValue}),
		alphanumeric: () => i18n(
				'Field value should contain letters or digits only.'),
		alphanumericWithSpaces: () => i18n(
				'Field value should contain letters, digits or spaces only.'),
		validateName: ()=> i18n(
				'Field value should contain English letters, digits , spaces, underscores, dashes and dots only.'),
		validateVendorName: ()=> i18n(
				'Field value should contain English letters digits and spaces only.'),
		freeEnglishText: ()=> i18n(
				'Field value should contain  English letters, digits , spaces, underscores, dashes and dots only.'),
		email: () => i18n('Field value should be a valid email address.'),
		ip: () => i18n('Field value should be a valid ip address.'),
		url: () => i18n('Field value should be a valid url address.'),
		general: () => i18n('Field value is invalid.')
};

class ValidationInput extends React.Component {
		
		static contextTypes = {
				validationParent: React.PropTypes.any,
				isReadOnlyMode: React.PropTypes.bool
		};
		
		static defaultProps = {
				onChange: null,
				disabled: null,
				didUpdateCallback: null,
				validations: {},
				value: ''
		};
		
		static propTypes = {
				onChange: React.PropTypes.func,
				disabled: React.PropTypes.bool,
				didUpdateCallback: React.PropTypes.func,
				validations: React.PropTypes.object
		};
		
		
		state = {
				isValid: true,
				style: null,
				value: this.props.value,
				error: {},
				previousErrorMessage: '',
				wasInvalid: false
		};
		
		componentWillReceiveProps({value: nextValue, validations: nextValidaions}) {
				if (this.state.wasInvalid) {
						const {validations, value} = this.props;
						if (value !== nextValue || !isEqual(validations, nextValidaions)) {
								this.validate(nextValue, nextValidaions);
						}
				} else if (this.props.value !== nextValue) {
						this.setState({value: nextValue});
				}
		}
		
		render() {
				let {isMultiSelect, onOtherChange, type} = this.props;
				
				let groupClasses = this.props.groupClassName || '';
				if (this.props.validations.required) {
						groupClasses += ' required';
				}
				let isReadOnlyMode = this.context.isReadOnlyMode;
				
				return (
						<div className='validation-input-wrapper'>
								{
										!isMultiSelect && !onOtherChange && type !== 'select'
										&& <Input
												{...this.props}
												groupClassName={groupClasses}
												ref={'_myInput'}
												value={this.state.value}
												disabled={isReadOnlyMode || Boolean(this.props.disabled)}
												bsStyle={this.state.style}
												onChange={() => this.changedInput()}
												onBlur={() => this.blurInput()}>
												{this.props.children}
										</Input>
								}
								{
										(isMultiSelect || onOtherChange || type === 'select')
										&& <InputOptions onInputChange={() => this.changedInput()}
										                 onBlur={() => this.blurInput()}
										                 hasError={!this.state.isValid}
										                 ref={'_myInput'} {...this.props} />
								}
								{this.renderOverlay()}
						</div>
				);
		}
		
		renderOverlay() {
				let position = 'right';
				if (this.props.type === 'text'
						|| this.props.type === 'email'
						|| this.props.type === 'number'
						|| this.props.type === 'password'
				
				) {
						position = 'bottom';
				}
				
				let validationMessage = this.state.error.message ||
						this.state.previousErrorMessage;
				return (
						<Overlay
								show={!this.state.isValid}
								placement={position}
								target={() => {let target = ReactDOM.findDOMNode(this.refs._myInput); return target.offsetParent ? target : undefined;}}
								container={this}>
								<Tooltip
										id={`error-${validationMessage.replace(' ','-')}`}
										className='validation-error-message'>
										{validationMessage}
								</Tooltip>
						</Overlay>
				);
		}
		
		componentDidMount() {
				if (this.context.validationParent) {
						this.context.validationParent.register(this);
				}
		}
		
		componentDidUpdate(prevProps, prevState) {
				if (this.context.validationParent) {
						if (prevState.isValid !== this.state.isValid) {
								this.context.validationParent.childValidStateChanged(this,
										this.state.isValid);
						}
				}
				if (this.props.didUpdateCallback) {
						this.props.didUpdateCallback();
				}
				
		}
		
		componentWillUnmount() {
				if (this.context.validationParent) {
						this.context.validationParent.unregister(this);
				}
		}
		
		/***
			* Adding same method as the actual input component
			* @returns {*}
			*/
		getValue() {
				if (this.props.type === 'checkbox') {
						return this.refs._myInput.getChecked();
				}
				return this.refs._myInput.getValue();
		}
		
		resetValue() {
				this.setState({value: this.props.value});
		}
		
		
		/***
			* internal method that validated the value. includes callback to the
			* onChange method
			* @param value
			* @param validations - map containing validation id and the limitation
			*   describing the validation.
			* @returns {object}
			*/
		validateValue = (value, validations) => {
				let {customValidationFunction} = validations;
				let error = {};
				let isValid = true;
				for (let validation in validations) {
						if ('customValidationFunction' !== validation) {
								if (validations[validation]) {
										if (!globalValidationFunctions[validation](value,
														validations[validation])) {
												error.id = validation;
												error.message =
														globalValidationMessagingFunctions[validation](value,
																validations[validation]);
												isValid = false;
												break;
										}
								}
						} else {
								let customValidationResult = customValidationFunction(value);
								
								if (customValidationResult !== true) {
										error.id = 'custom';
										isValid = false;
										if (typeof customValidationResult === 'string') {//custom validation error message supplied.
												error.message = customValidationResult;
										} else {
												error.message = globalValidationMessagingFunctions.general();
										}
										break;
								}
								
								
						}
				}
				
				return {
						isValid,
						error
				};
		};
		
		/***
			* Internal method that handles the change event of the input. validates and
			* updates the state.
			*/
		changedInput() {
				
				let {isValid, error} = this.state.wasInvalid ? this.validate() : this.state;
				let onChange = this.props.onChange;
				if (onChange) {
						onChange(this.getValue(), isValid, error);
				}
		};
		
		blurInput() {
				if (!this.state.wasInvalid) {
						this.setState({wasInvalid: true});
				}
				
				let {isValid, error} = !this.state.wasInvalid
						? this.validate()
						: this.state;
				let onBlur = this.props.onBlur;
				if (onBlur) {
						onBlur(this.getValue(), isValid, error);
				}
		};
		
		validate(value = this.getValue(), validations = this.props.validations) {
				let validationStatus = this.validateValue(value, validations);
				let {isValid, error} = validationStatus;
				let _style = isValid ? null : 'error';
				this.setState({
						isValid,
						error,
						value,
						previousErrorMessage: this.state.error.message || '',
						style: _style,
						wasInvalid: !isValid || this.state.wasInvalid
				});
				
				return validationStatus;
		}
		
		isValid() {
				return this.state.isValid;
		}
		
}
export default ValidationInput;