Skip to content

Commit

Permalink
fix for issue molgenis#3290. Cannot create TypeTest entity
Browse files Browse the repository at this point in the history
  • Loading branch information
erwinwinder committed Jul 2, 2015
1 parent 7d7c3d6 commit 9031a78
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
} else if(this.props.mode === 'create') {
return this.state.modal ? molgenis.ui.Form({
entity : this.state.entity.name,
showHidden : true,
cancelBtn : true,
modal: true,
onSubmitCancel : this._onModalHide,
Expand Down
152 changes: 78 additions & 74 deletions molgenis-core-ui/src/main/resources/js/component/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@
this._validate(attr, this._getValue(this.state.entityInstance, attr), function(validationResult) {
if (validationResult.valid === false) {
errorMessages[attr.name] = validationResult.errorMessage;
console.log(attr.name + ":" + validationResult.errorMessage);
}
resolve(validationResult.valid);
}.bind(this));
Expand All @@ -346,7 +347,7 @@
for (var i = 0; i < results.length && valid; i++) {
valid = valid && results[i];
}

console.log("valid=" + valid);
if (valid) {
$(target).closest('form').submit();//TODO remove jquery form submit workaround, see also componentDidMount in JQueryForm.js
} else {
Expand Down Expand Up @@ -431,80 +432,83 @@
var nullOrUndefinedValue = value === null || value === undefined;
var entityInstance = _.extend({}, this.state.entityInstance);
var errorMessage = undefined;
var computed = (attr.expression !== undefined);

if(attr.nillable === false && type !== 'CATEGORICAL_MREF' && type !== 'MREF' && nullOrUndefinedValue && !attr.auto) { // required value constraint
if (attr.visibleExpression === undefined || this._resolveBoolExpression(attr.visibleExpression, entityInstance) === true) {
errorMessage = 'Please enter a value.';
}
}
else if(attr.nillable === false && (type === 'CATEGORICAL_MREF' || type === 'MREF') && (nullOrUndefinedValue || value.items.length === 0)) { // required value constraint
errorMessage = 'Please enter a value.';
}
else if(type === 'EMAIL' && !nullOrUndefinedValue && !this._statics.REGEX_EMAIL.test(value)) {
errorMessage = 'Please enter a valid email address.';
}
else if(type === 'HYPERLINK' && !nullOrUndefinedValue && !this._statics.REGEX_URL.test(value)) {
errorMessage = 'Please enter a valid URL.';
}
else if(!attr.range && (type === 'INT' || type === 'LONG') && !nullOrUndefinedValue && !this._isInteger(value)) {
errorMessage = 'Please enter an integer value.';
}
else if(!attr.range && type === 'INT' && !nullOrUndefinedValue && !this._inRange(value, {min: this._statics.INT_MIN, max: this._statics.INT_MAX})) {
errorMessage = 'Please enter a value between ' + this._statics.INT_MIN + ' and ' + this._statics.INT_MAX + '.';
}
else if(!attr.range && type === 'LONG' && !nullOrUndefinedValue && !this._inRange(value, {min: this._statics.LONG_MIN, max: this._statics.LONG_MAX})) {
errorMessage = 'Please enter a value between ' + this._statics.LONG_MIN + ' and ' + this._statics.LONG_MAX + '.';
}
else if(attr.range && (type === 'INT' || type === 'LONG') && !nullOrUndefinedValue && !this._inRange(value, attr.range)) {
if(attr.range.min !== undefined && attr.range.max !== undefined) {
errorMessage = 'Please enter a value between ' + attr.range.min + ' and ' + attr.range.max + '.';
}
else if(attr.range.min !== undefined) {
errorMessage = 'Please enter a value greater than or equal to ' + attr.range.min + '.';
}
else if(attr.range.max !== undefined) {
errorMessage = 'Please enter a value lower than or equal to ' + attr.range.max + '.';
}
}
else if(attr.unique === true && !nullOrUndefinedValue) { // value uniqueness constraint

// determine query value
var queryValue;
switch(type) {
case 'CATEGORICAL':
case 'XREF':
queryValue = value[attr.refEntity.idAttribute];
break;
case 'CATEGORICAL_MREF':
case 'MREF':
queryValue = _.map(value, function(item) {
return item[attr.refEntity.idAttribute];
});
break;
default:
queryValue = value;
break;
}

// check if value already exists for this attribute
var rules = [{field: attr.name, operator: 'EQUALS', value: queryValue}];

api.getAsync(this.state.entity.hrefCollection, {q: {q: rules}}, function(data) {
var idAttribute = data.meta.idAttribute;
if(data.total > 0 && ((this.props.mode === 'create') || (data.items[0][idAttribute] !== this.state.entityInstance[idAttribute]))) {
callback({valid: false, errorMessage: 'This ' + attr.label + ' already exists. It must be unique.'});
} else {
callback({valid: true, errorMessage: undefined});
}
}.bind(this));
return;
}

if (attr.validationExpression) {
entityInstance[attr.name] = value;
if (this._resolveBoolExpression(attr.validationExpression, entityInstance) === false) {
errorMessage = 'Please enter a valid value.';
}
if (!computed) {//Do not validate computed attributes
if(attr.nillable === false && type !== 'CATEGORICAL_MREF' && type !== 'MREF' && nullOrUndefinedValue && !attr.auto) { // required value constraint
if (attr.visibleExpression === undefined || this._resolveBoolExpression(attr.visibleExpression, entityInstance) === true) {
errorMessage = 'Please enter a value.';
}
}
else if(attr.nillable === false && (type === 'CATEGORICAL_MREF' || type === 'MREF') && (nullOrUndefinedValue || value.items.length === 0)) { // required value constraint
errorMessage = 'Please enter a value.';
}
else if(type === 'EMAIL' && !nullOrUndefinedValue && !this._statics.REGEX_EMAIL.test(value)) {
errorMessage = 'Please enter a valid email address.';
}
else if(type === 'HYPERLINK' && !nullOrUndefinedValue && !this._statics.REGEX_URL.test(value)) {
errorMessage = 'Please enter a valid URL.';
}
else if(!attr.range && (type === 'INT' || type === 'LONG') && !nullOrUndefinedValue && !this._isInteger(value)) {
errorMessage = 'Please enter an integer value.';
}
else if(!attr.range && type === 'INT' && !nullOrUndefinedValue && !this._inRange(value, {min: this._statics.INT_MIN, max: this._statics.INT_MAX})) {
errorMessage = 'Please enter a value between ' + this._statics.INT_MIN + ' and ' + this._statics.INT_MAX + '.';
}
else if(!attr.range && type === 'LONG' && !nullOrUndefinedValue && !this._inRange(value, {min: this._statics.LONG_MIN, max: this._statics.LONG_MAX})) {
errorMessage = 'Please enter a value between ' + this._statics.LONG_MIN + ' and ' + this._statics.LONG_MAX + '.';
}
else if(attr.range && (type === 'INT' || type === 'LONG') && !nullOrUndefinedValue && !this._inRange(value, attr.range)) {
if(attr.range.min !== undefined && attr.range.max !== undefined) {
errorMessage = 'Please enter a value between ' + attr.range.min + ' and ' + attr.range.max + '.';
}
else if(attr.range.min !== undefined) {
errorMessage = 'Please enter a value greater than or equal to ' + attr.range.min + '.';
}
else if(attr.range.max !== undefined) {
errorMessage = 'Please enter a value lower than or equal to ' + attr.range.max + '.';
}
}
else if(attr.unique === true && !nullOrUndefinedValue) { // value uniqueness constraint

// determine query value
var queryValue;
switch(type) {
case 'CATEGORICAL':
case 'XREF':
queryValue = value[attr.refEntity.idAttribute];
break;
case 'CATEGORICAL_MREF':
case 'MREF':
queryValue = _.map(value, function(item) {
return item[attr.refEntity.idAttribute];
});
break;
default:
queryValue = value;
break;
}

// check if value already exists for this attribute
var rules = [{field: attr.name, operator: 'EQUALS', value: queryValue}];

api.getAsync(this.state.entity.hrefCollection, {q: {q: rules}}, function(data) {
var idAttribute = data.meta.idAttribute;
if(data.total > 0 && ((this.props.mode === 'create') || (data.items[0][idAttribute] !== this.state.entityInstance[idAttribute]))) {
callback({valid: false, errorMessage: 'This ' + attr.label + ' already exists. It must be unique.'});
} else {
callback({valid: true, errorMessage: undefined});
}
}.bind(this));
return;
}

if (attr.validationExpression) {
entityInstance[attr.name] = value;
if (this._resolveBoolExpression(attr.validationExpression, entityInstance) === false) {
errorMessage = 'Please enter a valid value.';
}
}
}

callback({valid: errorMessage === undefined, errorMessage: errorMessage});
Expand Down
2 changes: 2 additions & 0 deletions molgenis-core-ui/src/main/resources/js/component/Table.js
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,7 @@
return this.state.form ? molgenis.ui.Form({
entity: this.props.entity.name,
mode: 'create',
showHidden: true,
modal: true,
onSubmitSuccess: this._handleCreateConfirm,
onSubmitCancel: this._handleCreateCancel
Expand Down Expand Up @@ -805,6 +806,7 @@
entity : this.props.name,
entityInstance: this.props.id,
mode: 'edit',
showHidden: true,
modal: true,
onSubmitSuccess: this._handleEditConfirm,
onSubmitCancel: this._handleEditCancel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,7 @@
function getCreateForm(entityMetaData) {
React.render(molgenis.ui.Form({
mode: 'create',
showHidden: true,
entity : entityMetaData.name,
modal: true,
onSubmitSuccess: function() {
Expand All @@ -913,6 +914,7 @@
entity : settings.entityMetaData.name,
entityInstance: $(this).closest('tr').data('id'),
mode: 'edit',
showHidden: true,s
modal: true,
onSubmitSuccess : function() {
settings.start = 0;
Expand Down

0 comments on commit 9031a78

Please sign in to comment.