From 747c069c4579277393349c5f1f1589efb784118a Mon Sep 17 00:00:00 2001 From: Petr Vobornik Date: Mon, 2 Jan 2012 10:33:09 +0100 Subject: 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 --- install/ui/field.js | 138 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 87 insertions(+), 51 deletions(-) (limited to 'install') diff --git a/install/ui/field.js b/install/ui/field.js index 18a52c9b..fc6b75dd 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 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 || {}; -- cgit