From 525bf04da56829ac2b7b6e086a7a2d965727ce02 Mon Sep 17 00:00:00 2001 From: Petr Voborník Date: Wed, 22 Feb 2012 16:23:13 +0100 Subject: Making validators to return true result if empty All custom validators were changed to return true result if value is empty. Raising error if value is empty is resposibility of check_required call. This fixes immediate displaying of error message in multivalued fields containing custom validators. https://fedorahosted.org/freeipa/ticket/2351 --- install/ui/dns.js | 24 ++++++------------------ install/ui/field.js | 41 ++++++++++++++++++++++------------------- 2 files changed, 28 insertions(+), 37 deletions(-) (limited to 'install') diff --git a/install/ui/dns.js b/install/ui/dns.js index 8125f9d3b..ddd9da03b 100644 --- a/install/ui/dns.js +++ b/install/ui/dns.js @@ -2151,16 +2151,15 @@ IPA.ip_address_validator = function(spec) { that.validate = function(value) { + if (IPA.is_empty(value)) return that.true_result(); + var address = NET.ip_address(value); if (!address.valid || !that.is_type_match(address.type)) { - return { - valid: false, - message: that.message - }; + return that.false_result(); } - return { valid: true }; + return that.true_result(); }; that.is_type_match = function(net_type) { @@ -2203,21 +2202,10 @@ IPA.network_validator = function(spec) { that.specials = spec.specials || []; that.message = spec.message || IPA.messages.widget.validation.net_address; - that.false_result = function() { - return { - valid: false, - message: that.message - }; - }; - - that.true_result = function() { - return { - valid: true - }; - }; - that.validate = function(value) { + if (IPA.is_empty(value)) return that.true_result(); + if (typeof value !== 'string') return that.false_result(); if (that.specials.indexOf(value) > -1) { diff --git a/install/ui/field.js b/install/ui/field.js index 01a0c0f41..4f88c16e0 100644 --- a/install/ui/field.js +++ b/install/ui/field.js @@ -395,8 +395,23 @@ IPA.validator = function(spec) { var that = {}; + that.message = spec.message || IPA.messages.widget.validation.error; + + that.false_result = function(message) { + return { + valid: false, + message: message || that.message + }; + }; + + that.true_result = function() { + return { + valid: true + }; + }; + that.validate = function() { - return { valid: true }; + return that.true_result(); }; return that; @@ -411,46 +426,34 @@ IPA.metadata_validator = function(spec) { var message; var metadata = context.metadata; - if (!metadata) return { valid: true }; + if (!metadata || IPA.is_empty(value)) return that.true_result(); if (metadata.type == 'int') { if (!value.match(/^-?\d+$/)) { - return { - valid: false, - message: IPA.messages.widget.validation.integer - }; + return that.false_result(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 - }; + return that.false_result(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 - }; + return that.false_result(message); } } if (metadata.pattern) { var regex = new RegExp(metadata.pattern); if (!value.match(regex)) { - return { - valid: false, - message: metadata.pattern_errmsg - }; + return that.false_result(metadata.pattern_errmsg); } } - return { valid: true }; + return that.true_result(); }; return that; -- cgit