ValidationAwareTextInput.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. 'use strict';
  2. var textField = require('./TextInputEntryFactory');
  3. /**
  4. * This function is a wrapper around TextInputEntryFactory.
  5. * It adds functionality to cache an invalid value entered in the
  6. * text input, instead of setting it on the business object.
  7. */
  8. var validationAwareTextField = function(options, defaultParameters) {
  9. var modelProperty = options.modelProperty;
  10. defaultParameters.get = function(element, node) {
  11. var value = this.__lastInvalidValue;
  12. delete this.__lastInvalidValue;
  13. var properties = {};
  14. properties[modelProperty] = value !== undefined ? value : options.getProperty(element, node);
  15. return properties;
  16. };
  17. defaultParameters.set = function(element, values, node) {
  18. var validationErrors = validate.apply(this, [ element, values, node ]),
  19. propertyValue = values[modelProperty];
  20. // make sure we do not update the id
  21. if (validationErrors && validationErrors[modelProperty]) {
  22. this.__lastInvalidValue = propertyValue;
  23. return options.setProperty(element, {}, node);
  24. } else {
  25. var properties = {};
  26. properties[modelProperty] = propertyValue;
  27. return options.setProperty(element, properties, node);
  28. }
  29. };
  30. var validate = defaultParameters.validate = function(element, values, node) {
  31. var value = values[modelProperty] || this.__lastInvalidValue;
  32. var property = {};
  33. property[modelProperty] = value;
  34. return options.validate(element, property, node);
  35. };
  36. return textField(options, defaultParameters);
  37. };
  38. module.exports = validationAwareTextField;