@ -89,12 +89,15 @@
});
},
change: function (name) {
this.changeInternal(name, false);
},
changeInternal: function (name, submit) {
var vm = this;
var value = this.data.model[name];
var property = this.data.schema.properties[name];
this.validInternal(property.required, name, o => value);
if (value) {
this.validInternal(property.email, name, o => /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/.test(value));
this.validInternal(property.url, name, o => /^(?:(?:(?:https?|ftp):)?\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})).?)(?::\d{2,5})?(?:[/?#]\S*)?$/i.test(value));
this.validInternal(property.date, name, o => !/Invalid|NaN/.test(new Date(value).toString()));
@ -109,41 +112,46 @@
this.validInternal(property.rangelength, name, o => value.length >= property.length.min & & value.length < = property.length.max);
this.validInternal(property.range, name, o => parseFloat(value) >= property.range.min & & parseFloat(value) < = property.range.max);
this.validInternal(property.equalTo, name, o => o === vm.data.model[vm.camelCased(name)]);
}
this.validInternal(property.remote, name, o => {
var url = property.remote.url + '?' + name + '=' + value;
if (property.remote.additionalFields) {
var keys = property.remote.additionalFields.split(',');
for (var i = 0; i < keys.length ; i + + ) {
var key = keys[i ];
url = url + '& ' + key + '=' + vm.data.model[vm.camelCased(key)];
this.validInternal(property.remote, name, async o => {
var url = property.remote.url + '?' + name + '=' + value;
if (property.remote.additionalFields) {
var keys = property.remote.additionalFields.split(',');
for (var i = 0; i < keys.length ; i + + ) {
var key = keys[i];
url = url + '& ' + key + '=' + vm.data.model[vm.camelCased(key) ];
}
}
}
axios.get(url).then(function (response) {
vm.removeError(name);
if (response.data !== true) {
var message = response.data === false ? property.remote.message : response.data;
vm.updateError(name, message);
}
}).catch(function (error) {
vm.removeError(name);
vm.updateError(name, 'network error');
});
return false;
}, 'loading');
console.log(this.data.errors);
axios.get(url).then(function (response) {
if (response.data !== true) {
vm.updateError(name, 'error');
}
else {
vm.removeError(name,'loading');
}
vm.removeError(name,'loading');
}).catch(function () {
vm.updateError(name, 'error');
});
return false;
},'loading');
}
},
valid: function () {
var vm = this
_.forIn(this.data.schema.properties, function (value, key) {
vm.changeInternal(key,true);
});
return this.data.errors.length === 0;
},
validInternal: function (validator, name, valid, message) {
if (!validator) {
return;
}
var property = this.data.schema.properties[name];
message = message || validator.message ;
if (valid()) {
this.removeError(name);
this.removeError(name,message );
}
else {
message = message || validator.message;
this.updateError(name, message);
}
},
@ -153,8 +161,13 @@
removeError: function (name, message) {
var error = Enumerable.from(this.data.errors).firstOrDefault(o => o.key.toLowerCase() === name.toLowerCase());
if (error) {
var index = _.findIndex(this.data.errors, o => o.key.toLowerCase() === name.toLowerCase());
this.data.errors.splice(index, 1);
if (Enumerable.from(error.value.errors).any(o => o.errorMessage === message)) {
error.value.errors = Enumerable.from(error.value.errors).where(o => o.errorMessage !== message).toArray();
}
if (error.value.errors.length === 0) {
var index = _.findIndex(this.data.errors, o => o.key.toLowerCase() === name.toLowerCase());
this.data.errors.splice(index, 1);
}
}
},
updateError: function (name, message) {
@ -174,13 +187,6 @@
}
}
},
valid: function () {
var vm = this
_.forIn(this.data.schema.properties, function (value, key) {
vm.change(key);
});
return this.data.errors.length === 0;
},
getValidationSummary: function (excludePropertyErrors) {
var query = Enumerable.from(this.data.errors);
if (excludePropertyErrors) {
@ -215,20 +221,24 @@
return key !== 'id';
},
onSubmit: function (e) {
if (this.valid()) {
var vm = this;
axios.post(e.target.action, this.data.model).then(function (response) {
if (response.status === 204) {
var url = '/router/shared/list.html?entity=' + vm.entity;
setTimeout(function () {
router.push(url);
}, 1000);
}
else {
vm.data = response.data;
}
});
}
var vm = this;
new Promise(function (resolve, reject) {
vm.valid();
}).then(function () {
if (vm.data.errors.length === 0) {
axios.post(e.target.action, vm.data.model).then(function (response) {
if (response.status === 204) {
var url = '/router/shared/list.html?entity=' + vm.entity;
setTimeout(function () {
router.push(url);
}, 1000);
}
else {
vm.data = response.data;
}
});
}
});
}
}
}