Do validation before enabling the Save button.

This commit is contained in:
Ashesh Vashi 2015-12-04 15:33:16 +05:30
parent 537df154fe
commit 57d6c3b406
3 changed files with 74 additions and 6 deletions

View File

@ -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) {

View File

@ -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);

View File

@ -128,6 +128,32 @@
'</div>'
].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(
$("<div></div>").addClass('pgadmin-control-error-message col-xs-12 help-block').text(error)
);
});
};
var ReadonlyOptionControl = Backform.ReadonlyOptionControl = Backform.SelectControl.extend({
template: _.template([
'<label class="<%=Backform.controlLabelClassName%>"><%=label%></label>',