summaryrefslogtreecommitdiffstats
path: root/install/ui/field.js
diff options
context:
space:
mode:
authorPetr Vobornik <pvoborni@redhat.com>2012-01-02 10:33:09 +0100
committerEndi S. Dewata <edewata@redhat.com>2012-01-03 21:28:41 -0600
commit747c069c4579277393349c5f1f1589efb784118a (patch)
tree9e1d77216317da0b8f0fe74144bc12e31621084e /install/ui/field.js
parent9beb467d98cb16e09fcda5ebbeb27056dfff3a2d (diff)
downloadfreeipa-747c069c4579277393349c5f1f1589efb784118a.tar.gz
freeipa-747c069c4579277393349c5f1f1589efb784118a.tar.xz
freeipa-747c069c4579277393349c5f1f1589efb784118a.zip
Added support of custom field validators
Current validation logic supports only validation based on metadata. It can be extended only by overriding field's validation method. This approach requires creating subclasses of field for each different format of desired value. It's inconvenient for cases like adding the same validation logic to two different subclasses of field. This patch is adding support for creating custom validators. Validator is an object which contains validation logic. Validation is executed in a validate(value, context) method. This method checks if the value is valid and returns a validation result. Validation result is a simple object which contains valid property and an error message if valid is false. Field is extended by validators property. It can be set in spec object or later. It should contain instances of validators for the field. Validators are run in field's validation method. This patch is a prerequisite for: https://fedorahosted.org/freeipa/ticket/1466
Diffstat (limited to 'install/ui/field.js')
-rw-r--r--install/ui/field.js138
1 files changed, 87 insertions, 51 deletions
diff --git a/install/ui/field.js b/install/ui/field.js
index 18a52c9b6..fc6b75dda 100644
--- a/install/ui/field.js
+++ b/install/ui/field.js
@@ -53,6 +53,7 @@ IPA.field = function(spec) {
that.join = spec.join;
that.metadata = spec.metadata;
+ that.validators = spec.validators || [];
that.priority = spec.priority;
@@ -74,6 +75,8 @@ IPA.field = function(spec) {
that.tooltip = that.metadata.doc;
}
}
+
+ that.validators.push(IPA.metadata_validator());
};
that.is_required = function() {
@@ -98,71 +101,37 @@ IPA.field = function(spec) {
that.validate_required = function() {
var values = that.save();
- if (!values || !values.length || values[0] === '') {
- if (that.is_required()) {
- that.valid = false;
- that.show_error(IPA.messages.widget.validation.required);
- return false;
- }
+ if (that.is_empty(values) && that.is_required()) {
+ that.valid = false;
+ that.show_error(IPA.messages.widget.validation.required);
+ return false;
}
return true;
};
- /*returns true and clears the error message if the field value passes
- * the validation pattern. If the field value does not pass validation,
- * displays the error message and returns false. */
+ /**
+ * Returns true and clears the error message if the field value passes
+ * the validation pattern. If the field value does not pass validation,
+ * displays the error message and returns false.
+ */
that.validate = function() {
that.hide_error();
that.valid = true;
var values = that.save();
- if (!values) {
- return that.valid;
- }
- if (values.length === 0) {
- return that.valid;
- }
- var value = values[0];
- if (!value) {
- return that.valid;
- }
- if (!that.metadata) {
+ if (that.is_empty(values)) {
return that.valid;
}
- var message;
-
- if (that.metadata.type == 'int') {
- if (!value.match(/^-?\d+$/)) {
- that.valid = false;
- that.show_error(IPA.messages.widget.validation.integer);
- return that.valid;
- }
-
- if (that.metadata.minvalue !== undefined && value < that.metadata.minvalue) {
- that.valid = false;
- message = IPA.messages.widget.validation.min_value;
- message = message.replace('${value}', that.metadata.minvalue);
- that.show_error(message);
- return that.valid;
- }
+ var value = values[0];
- if (that.metadata.maxvalue !== undefined && value > that.metadata.maxvalue) {
- that.valid = false;
- message = IPA.messages.widget.validation.max_value;
- message = message.replace('${value}', that.metadata.maxvalue);
- that.show_error(message);
- return that.valid;
- }
- }
-
- if (that.metadata.pattern) {
- var regex = new RegExp(that.metadata.pattern);
- if (!value.match(regex)) {
- that.valid = false;
- that.show_error(that.metadata.pattern_errmsg);
- return that.valid;
+ for (var i=0; i<that.validators.length; i++) {
+ var validation_result = that.validators[i].validate(value, that);
+ that.valid = validation_result.valid;
+ if (!that.valid) {
+ that.show_error(validation_result.message);
+ break;
}
}
@@ -403,6 +372,73 @@ IPA.field = function(spec) {
return that;
};
+IPA.validator = function(spec) {
+
+ spec = spec || {};
+
+ var that = {};
+
+ that.validate = function() {
+ return { valid: true };
+ };
+
+ return that;
+};
+
+IPA.metadata_validator = function(spec) {
+
+ var that = IPA.validator(spec);
+
+ that.validate = function(value, context) {
+
+ var message;
+ var metadata = context.metadata;
+
+ if (!metadata) return { valid: true };
+
+ if (metadata.type == 'int') {
+ if (!value.match(/^-?\d+$/)) {
+ return {
+ valid: false,
+ message: IPA.messages.widget.validation.integer
+ };
+ }
+
+ if (metadata.minvalue !== undefined && value < metadata.minvalue) {
+ message = IPA.messages.widget.validation.min_value;
+ message = message.replace('${value}', metadata.minvalue);
+ return {
+ valid: false,
+ message: message
+ };
+ }
+
+ if (metadata.maxvalue !== undefined && value > metadata.maxvalue) {
+ message = IPA.messages.widget.validation.max_value;
+ message = message.replace('${value}', metadata.maxvalue);
+ return {
+ valid: false,
+ message: message
+ };
+ }
+ }
+
+ if (metadata.pattern) {
+ var regex = new RegExp(metadata.pattern);
+ if (!value.match(regex)) {
+ return {
+ valid: false,
+ message: metadata.pattern_errmsg
+ };
+ }
+ }
+
+ return { valid: true };
+ };
+
+ return that;
+};
+
IPA.checkbox_field = function(spec) {
spec = spec || {};