From 3442cac38b9d7f5fa7dac8739eb9422d41efc53d Mon Sep 17 00:00:00 2001 From: Scott Baker Date: Wed, 26 Nov 2014 00:53:19 -0800 Subject: [PATCH] validation of URL fields --- planetstack/core/models/plcorebase.py | 4 ++++ .../core/xoslib/static/js/xoslib/xos-util.js | 18 ++++++++++++------ .../core/xoslib/static/js/xoslib/xosHelper.js | 3 ++- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/planetstack/core/models/plcorebase.py b/planetstack/core/models/plcorebase.py index dee8a87..3bceb08 100644 --- a/planetstack/core/models/plcorebase.py +++ b/planetstack/core/models/plcorebase.py @@ -94,6 +94,8 @@ class DiffModelMixIn: l = [] if field.blank==False: l.append("notBlank") + if field.__class__.__name__=="URLField": + l.append("url") validators[field.name] = l return validators @@ -144,6 +146,8 @@ class PlCoreBase(models.Model): # , DiffModelMixIn): l = [] if field.blank==False: l.append("notBlank") + if field.__class__.__name__=="URLField": + l.append("url") validators[field.name] = l return validators diff --git a/planetstack/core/xoslib/static/js/xoslib/xos-util.js b/planetstack/core/xoslib/static/js/xoslib/xos-util.js index bebc44a..d03ab14 100644 --- a/planetstack/core/xoslib/static/js/xoslib/xos-util.js +++ b/planetstack/core/xoslib/static/js/xoslib/xos-util.js @@ -23,13 +23,19 @@ function limitTableRows(tableSelector, maxRows) { } function validateField(validatorName, value) { + if (validatorName=="notBlank") { + if ((value==undefined) || (value=="")) { + return "can not be blank"; + } + } + + // if notBlank wasn't set, and the field is blank, then we can return + if ((value==undefined) || (value=="")) { + return true; + } + switch (validatorName) { - case "notBlank": - if ((value==undefined) || (value=="")) { - return "can not be blank"; - } - break; - case "isUrl": + case "url": if (! /^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(value)) { return "must be a valid url"; } diff --git a/planetstack/core/xoslib/static/js/xoslib/xosHelper.js b/planetstack/core/xoslib/static/js/xoslib/xosHelper.js index 30835c7..dc66e45 100644 --- a/planetstack/core/xoslib/static/js/xoslib/xosHelper.js +++ b/planetstack/core/xoslib/static/js/xoslib/xosHelper.js @@ -369,12 +369,13 @@ XOSDetailView = Marionette.ItemView.extend({ }, onFormDataInvalid: function(errors) { + this.$el.find(".help-inline").remove(); var self=this; var markErrors = function(value, key) { console.log("name='" + key + "'"); var $inputElement = self.$el.find("[name='" + key + "']"); var $inputContainer = $inputElement.parent(); - $inputContainer.find(".help-inline").remove(); + //$inputContainer.find(".help-inline").remove(); var $errorEl = $("", {class: "help-inline error", text: value}); $inputContainer.append($errorEl).addClass("error"); } -- 2.43.0