diff --git a/web/pgadmin/browser/server_groups/servers/templates/servers/servers.js b/web/pgadmin/browser/server_groups/servers/templates/servers/servers.js index 75f7d9ef9..3e28423f2 100644 --- a/web/pgadmin/browser/server_groups/servers/templates/servers/servers.js +++ b/web/pgadmin/browser/server_groups/servers/templates/servers/servers.js @@ -194,13 +194,25 @@ function($, _, S, pgAdmin, pgBrowser, alertify) { {label: '{{ _('Unknown') }}', value: ''} ] }], - validate: function(attrs, options) { - if (!this.isNew() && 'id' in this.changed) { - return '{{ _('Id can not be changed!') }}'; + validate: function() { + var err = {}, + errmsg; + + if (!this.isNew() && 'id' in this.sessAttrs) { + var msg = '{{ _('Id can not be changed!') }}'; + err['id'] = msg; + errmsg = msg; } - if (String(this.name).replace(/^\s+|\s+$/g, '') == '') { - return '{{ _('Name can be empty!') }}'; + if (_.isUndefined(this.get('name')) || String(this.get('name')).replace(/^\s+|\s+$/g, '') == '') { + err['name'] = '{{ _('Name can be empty!') }}'; + errmsg = errmsg || msg; } + this.errorModel.set(err); + + if (_.size(err)) { + return err; + } + return null; }, isConnected: function(model) { diff --git a/web/pgadmin/browser/templates/browser/js/node.js b/web/pgadmin/browser/templates/browser/js/node.js index 214635ef5..b09a8ad19 100644 --- a/web/pgadmin/browser/templates/browser/js/node.js +++ b/web/pgadmin/browser/templates/browser/js/node.js @@ -580,7 +580,7 @@ function($, _, S, pgAdmin, Menu, Backbone, Alertify, Backform) { btnSave = btnGroup.find('button[type="save"]'), btnReset = btnGroup.find('button[type="reset"]'); - if (m.sessChanged()) { + if (m.sessValid() && m.sessChanged()) { btnSave.prop('disabled', false); btnSave.removeAttr('disabled'); btnReset.prop('disabled', false); @@ -978,6 +978,7 @@ function($, _, S, pgAdmin, Menu, Backbone, Alertify, Backform) { self.trackChanges = true; self.sessAttrs = { + 'valid': true, 'changed': [], 'added': [], 'deleted': [] @@ -1007,6 +1008,26 @@ function($, _, S, pgAdmin, Menu, Backbone, Alertify, Backform) { this.sessAttrs['deleted'].length > 0 ); }, + sessValid: function() { + _.each(this.sessAttrs['added'], function(o) { + if ('sessValid' in o && _.isFunction(o.sessValid) && + (!o.sessValid.apply(o) || + ('validate' in o && _.isFunction(o.validate) && + _.isString(o.validate.apply(o))))) { + return false; + } + return true; + }); + _.each(self.sessAttrs['changed'], function(o) { + if ('sessValid' in o && _.isFunction(o.sessValid) && + (!o.sessValid.apply(o) || + ('validate' in o && _.isFunction(o.validate) && + _.isString(o.validate.apply(o))))) { + return false; + } + }); + return true; + }, toJSON: function(session) { var self = this, onlyChanged = (typeof(session) != "undefined" && @@ -1061,6 +1082,7 @@ function($, _, S, pgAdmin, Menu, Backbone, Alertify, Backform) { return self.onChange(); } self.sessAttrs['added'].push(obj); + return self.onChange(); }, onModelRemove: function(obj) { @@ -1239,6 +1261,14 @@ function($, _, S, pgAdmin, Menu, Backbone, Alertify, Backform) { return self.get(o).sessChanged(); })); }, + sessValid: function() { + var self = this; + if ('validate' in self && _.isFunction(self.validate) && + _.isString(self.validate.apply(self))) { + return false; + } + return true; + }, set: function(key, val, options) { var res = Backbone.Model.prototype.set.call(this, key, val, options); diff --git a/web/pgadmin/static/js/backform.pgadmin.js b/web/pgadmin/static/js/backform.pgadmin.js index a2af1780f..716ac438c 100644 --- a/web/pgadmin/static/js/backform.pgadmin.js +++ b/web/pgadmin/static/js/backform.pgadmin.js @@ -128,6 +128,32 @@ '' ].join("\n")); + Backform.Control.prototype.clearInvalid = function() { + this.$el.removeClass(Backform.errorClassName); + this.$el.find(".pgadmin-control-error-message").remove(); + return this; + }; + Backform.Control.prototype.updateInvalid = function() { + var self = this; + var errorModel = this.model.errorModel; + if (!(errorModel instanceof Backbone.Model)) return this; + + this.clearInvalid(); + + this.$el.find(':input').not('button').each(function(ix, el) { + var attrArr = $(el).attr('name').split('.'), + name = attrArr.shift(), + path = attrArr.join('.'), + error = self.keyPathAccessor(errorModel.toJSON(), $(el).attr('name')); + + if (_.isEmpty(error)) return; + + self.$el.addClass(Backform.errorClassName).append( + $("
").addClass('pgadmin-control-error-message col-xs-12 help-block').text(error) + ); + }); + }; + var ReadonlyOptionControl = Backform.ReadonlyOptionControl = Backform.SelectControl.extend({ template: _.template([ '',