ValidationAwareTextInput.js
1.54 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
55
56
'use strict';
var textField = require('./TextInputEntryFactory');
/**
* This function is a wrapper around TextInputEntryFactory.
* It adds functionality to cache an invalid value entered in the
* text input, instead of setting it on the business object.
*/
var validationAwareTextField = function(options, defaultParameters) {
var modelProperty = options.modelProperty;
defaultParameters.get = function(element, node) {
var value = this.__lastInvalidValue;
delete this.__lastInvalidValue;
var properties = {};
properties[modelProperty] = value !== undefined ? value : options.getProperty(element, node);
return properties;
};
defaultParameters.set = function(element, values, node) {
var validationErrors = validate.apply(this, [ element, values, node ]),
propertyValue = values[modelProperty];
// make sure we do not update the id
if (validationErrors && validationErrors[modelProperty]) {
this.__lastInvalidValue = propertyValue;
return options.setProperty(element, {}, node);
} else {
var properties = {};
properties[modelProperty] = propertyValue;
return options.setProperty(element, properties, node);
}
};
var validate = defaultParameters.validate = function(element, values, node) {
var value = values[modelProperty] || this.__lastInvalidValue;
var property = {};
property[modelProperty] = value;
return options.validate(element, property, node);
};
return textField(options, defaultParameters);
};
module.exports = validationAwareTextField;