Skip to content

Commit

Permalink
Add UI options validation. Closes #1023
Browse files Browse the repository at this point in the history
  • Loading branch information
wellingguzman committed Aug 19, 2016
1 parent 04fc63a commit 4467ccb
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 12 deletions.
1 change: 1 addition & 0 deletions app/modules/settings/views/TablesView.js
Expand Up @@ -1047,6 +1047,7 @@ function(app, Backbone, Directus, BasePageView, TableModel, ColumnModel, UIManag
var model = column.options;
model.set({id: column.get('ui')});
var schema = app.schemaManager.getColumns('ui', model.id);
model.structure = schema;
var view = new EditColumn({model: model, schema: schema});
app.router.overlayPage(view);
view.save = function() {
Expand Down
72 changes: 60 additions & 12 deletions app/schema/UIModel.js
Expand Up @@ -2,10 +2,23 @@ define(function(require, exports, module) {

"use strict";

var app = require('app');
var _ = require('underscore');
var Backbone = require('backbone');
var UIManager = require('core/UIManager');

module.exports = Backbone.Model.extend({

inputs: {},

addInput: function(attr, input) {
this.inputs[attr] = input;
},

getInput: function(attr) {
return this.inputs[attr];
},

url: function() {
var column = this.parent;
var columnSchema = this.parent.collection;
Expand All @@ -15,7 +28,7 @@ define(function(require, exports, module) {

// When the time is right, this part need serious reconsideration
getStructure: function() {
return this.parent.structure;
return this.structure;
},

getTable: function() {
Expand All @@ -25,23 +38,58 @@ define(function(require, exports, module) {
//@todo: This is code repetition. Almost identical to entries.model. Create a mixin?
validate: function(attributes, options) {
var errors = [];
//var structure = this.getStructure();
var structure = this.getStructure();

// When this model is created the structure may not be defined.
if (!structure) {
return;
}

//only validates attributes that are part of the schema
//attributes = _.pick(attributes, structure.pluck('id'));
var isNothing = function(value) {
return value === undefined || value === null || value === '' || (!app.isNumber(value) && !_.isDate(value) && _.isEmpty(value));
};

/*
@todo: Fix this. Validation does not work!
// only validates attributes that are part of the schema
attributes = _.pick(attributes, structure.pluck('id'));
_.each(attributes, function(value, key, list) {
var mess = ui.validate(this, key, value);
var column = structure.get(key);

// Don't validate hidden fields
// @todo should this be adjusted since these fields are now posting in some cases?
if (column.get('hidden_input')) {
return;
}

// Don't validate ID
if (key === 'id') {
return;
}

var nullDisallowed = column.get('is_nullable') === 'NO';
var ui = UIManager._getUI(column.get('ui'));
var forceUIValidation = ui.forceUIValidation === true;
var isNull = isNothing(value);
var uiSettings = UIManager.getSettings(column.get('ui'));
var skipSerializationIfNull = uiSettings.skipSerializationIfNull;
var mess = (!forceUIValidation && !skipSerializationIfNull && nullDisallowed && isNull) ?
'The field cannot be empty'
: UIManager.validate(this, key, value);

if (mess !== undefined) {
errors.push({attr: key, message: ui.validate(this, key, value)});
errors.push({attr: key, message: mess});
}
}, this);
*/

if (errors.length > 0) return errors;
}
},

initialize: function() {
this.on('invalid', function(model, errors) {
var details = _.map(errors, function(err) { return '<b>'+app.capitalize(err.attr)+':</b> '+err.message; }).join('</li><li>');
var error_id = (this.id)? this.id : 'New';
details = app.capitalize(this.parent.get('column_name')) + ' (' + error_id + ')' + '<hr><ul><li>' + details + '</li></ul>';
app.trigger('alert:error', 'There seems to be a problem...', details);
});
},
});

});
});

0 comments on commit 4467ccb

Please sign in to comment.