ldx-widgets
Version:
widgets
66 lines (57 loc) • 1.9 kB
JavaScript
(function() {
var React, li;
React = require('react');
li = require('react-dom-factories').li;
/*
Form Validation
@props.showValidationErrors - OPTIONAL - Boolean
indicator to determine whether validation errors should be visible on the element
@props.validation - OPTIONAL - Array
Array of validation evaluation rules. Required if validation is being used.
*/
module.exports = {
elementValid: true,
validationErrors: [],
validationErrorIds: [],
getDefaultProps: function() {
return {
validation: [],
showValidationErrors: false
};
},
isValid: function() {
return this.elementValid;
},
getErrors: function() {
var error, errors, evaluation, i, id, index, len, ref, showValidationErrors, validation;
ref = this.props, validation = ref.validation, showValidationErrors = ref.showValidationErrors;
this.validationErrors = [];
this.validationErrorIds = [];
errors = [];
if (validation != null) {
for (index = i = 0, len = validation.length; i < len; index = ++i) {
evaluation = validation[index];
if (!((evaluation != null ? evaluation.valid : void 0) === false)) {
continue;
}
error = evaluation.error, id = evaluation.id;
if (id) {
this.validationErrorIds.push(id);
}
this.validationErrors.push(li({
className: 'field-error',
key: error
}, error));
this.validateElement();
}
}
return this.invalidClass = this.validationErrors.length && showValidationErrors ? ' invalid' : '';
},
validateElement: function() {
return this.elementValid = this.validationErrors.length === 0;
},
componentDidUpdate: function() {
return this.validateElement();
}
};
}).call(this);