diff --git a/web/pgadmin/browser/templates/browser/js/browser.js b/web/pgadmin/browser/templates/browser/js/browser.js index ccf3217..ef18358 100644 --- a/web/pgadmin/browser/templates/browser/js/browser.js +++ b/web/pgadmin/browser/templates/browser/js/browser.js @@ -634,6 +634,7 @@ function(require, $, _, S, Bootstrap, pgAdmin, alertify, CodeMirror) { 'SQL_TAB': '{{ _('SQL') }}', 'SQL_NO_CHANGE': '\n -- ' + '{{ _('Nothing changed') }}', 'MUST_BE_INT' : " {{ _("'%%s' must be an integer.") }}", + 'MUST_BE_NUM' : " {{ _("'%%s' must be a numeric.") }}", 'MUST_GR_EQ' : " {{ _("'%%s' must be greater than or equals to %%d.") }}", 'MUST_LESS_EQ' : " {{ _("'%%s' must be less than or equals to %%d.") }}" } diff --git a/web/pgadmin/static/js/backform.pgadmin.js b/web/pgadmin/static/js/backform.pgadmin.js index 8ffaecb..84f1c04 100644 --- a/web/pgadmin/static/js/backform.pgadmin.js +++ b/web/pgadmin/static/js/backform.pgadmin.js @@ -67,7 +67,7 @@ var controlMapper = Backform.controlMapper = { 'int': ['uneditable-input', 'integer', 'integer'], 'text': ['uneditable-input', 'input', 'string'], - 'numeric': ['uneditable-input', 'input', 'number'], + 'numeric': ['uneditable-input', 'numeric', 'numeric'], 'date': 'datepicker', 'boolean': 'boolean', 'options': ['readonly-option', 'select', Backgrid.Extension.PGSelectCell], @@ -1252,6 +1252,109 @@ this.stopListening(this.model, "change:" + name, this.render); this.model.set(name, value); this.listenTo(this.model, "change:" + name, this.render); + } else { + if (this.model.collection || this.model.handler) { + (this.model.collection || this.model.handler).trigger( + 'pgadmin-session:model:invalid', this.model.errorModel.get(name), this.model + ); + } else { + (this.model).trigger( + 'pgadmin-session:invalid', this.model.errorModel.get(name), this.model + ); + } + } + } + }); + + /* + * Numeric input Control functionality just like backgrid + */ + var NumericControl = Backform.NumericControl = Backform.InputControl.extend({ + defaults: { + type: "number", + label: "", + min: undefined, + max: undefined, + maxlength: 255, + extraClasses: [], + helpMessage: null + }, + template: _.template([ + '', + '
', + ' <%=required ? "required" : ""%> />', + ' <% if (helpMessage && helpMessage.length) { %>', + ' <%=helpMessage%>', + ' <% } %>', + '
' + ].join("\n")), + events: { + "change input": "checkNumeric", + "focus input": "clearInvalid" + }, + checkNumeric: function(e) { + var field = _.defaults(this.field.toJSON(), this.defaults), + attrArr = this.field.get("name").split('.'), + name = attrArr.shift(), + value = this.getValueFromDOM(), + min_value = field.min, + max_value = field.max, + isValid = true, + intPattern = new RegExp("^-?[0-9]+(\.?[0-9]*)?$"), + isMatched = intPattern.test(value); + + // Below logic will validate input + if (!isMatched) { + isValid = false; + this.model.errorModel.unset(name); + this.model.errorModel.set( + name, + S(pgAdmin.Browser.messages.MUST_BE_NUM).sprintf( + field.label + ).value() + ); + } + + // Below will check if entered value is in-between min & max range + if (isValid && (!_.isUndefined(min_value) && value < min_value)) { + isValid = false; + this.model.errorModel.unset(name); + this.model.errorModel.set( + name, + S(pgAdmin.Browser.messages.MUST_GR_EQ).sprintf( + field.label, + min_value + ).value() + ); + } + + if (isValid && (!_.isUndefined(max_value) && value > max_value)) { + isValid = false; + this.model.errorModel.unset(name); + this.model.errorModel.set( + name, + S(pgAdmin.Browser.messages.MUST_LESS_EQ).sprintf( + field.label, + max_value + ).value() + ); + } + + // After validation we need to set that value into model (only if all flags are true) + if (isValid) { + this.stopListening(this.model, "change:" + name, this.render); + this.model.set(name, value); + this.listenTo(this.model, "change:" + name, this.render); + } else { + if (this.model.collection || this.model.handler) { + (this.model.collection || this.model.handler).trigger( + 'pgadmin-session:model:invalid', this.model.errorModel.get(name), this.model + ); + } else { + (this.model).trigger( + 'pgadmin-session:invalid', this.model.errorModel.get(name), this.model + ); + } } } });