Former-commit-id: e12a89f36956ce72a6c1ff3ef9aa4c7484f5ad25
Former-commit-id: 904f17d13ce2ac7ee44e1f5a8de756cd76d8f2dc
TSXN
wanggang 5 years ago
parent c37a446925
commit b2ddc777a0

@ -31,7 +31,7 @@ namespace Application.Models
[MaxLength(64, ErrorMessage = "{0}最大长度为{1}")] [MaxLength(64, ErrorMessage = "{0}最大长度为{1}")]
[Required(ErrorMessage = nameof(RequiredAttribute))] [Required(ErrorMessage = nameof(RequiredAttribute))]
//for temp test start //for temp test start
//[Remote("Valid", "Ajax", "Admin", AdditionalFields = "CategoryNumber", ErrorMessage = "test ajax valid")] [Remote("Valid", "Node", "Admin", AdditionalFields = "CategoryNumber", ErrorMessage = "test ajax valid")]
//[Remote("CronValid", "Ajax", "Admin")] //[Remote("CronValid", "Ajax", "Admin")]
//for temp test end //for temp test end
public string Name { get; set; } public string Name { get; set; }

@ -9,6 +9,7 @@ using Microsoft.EntityFrameworkCore;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks;
namespace IoT.Shared.Areas.Admin.Controlls namespace IoT.Shared.Areas.Admin.Controlls
{ {
@ -20,6 +21,12 @@ namespace IoT.Shared.Areas.Admin.Controlls
{ {
} }
public async Task<IActionResult> Valid(string name)
{
await Task.Delay(3000);
return Json(name=="123");
}
public override string GetNodeNumber(EditNodeModel model) public override string GetNodeNumber(EditNodeModel model)
{ {
return model.Number; return model.Number;

@ -89,6 +89,9 @@
}); });
}, },
change: function (name) { change: function (name) {
this.changeInternal(name, false);
},
changeInternal: function (name, submit) {
var vm = this; var vm = this;
var value = this.data.model[name]; var value = this.data.model[name];
var property = this.data.schema.properties[name]; var property = this.data.schema.properties[name];
@ -109,41 +112,46 @@
this.validInternal(property.rangelength, name, o => value.length >= property.length.min && value.length <= property.length.max); 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.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.equalTo, name, o => o === vm.data.model[vm.camelCased(name)]);
} this.validInternal(property.remote, name, async o => {
this.validInternal(property.remote, name, o => { var url = property.remote.url + '?' + name + '=' + value;
var url = property.remote.url + '?' + name + '=' + value; if (property.remote.additionalFields) {
if (property.remote.additionalFields) { var keys = property.remote.additionalFields.split(',');
var keys = property.remote.additionalFields.split(','); for (var i = 0; i < keys.length; i++) {
for (var i = 0; i < keys.length; i++) { var key = keys[i];
var key = keys[i]; url = url + '&' + key + '=' + vm.data.model[vm.camelCased(key)];
url = url + '&' + key + '=' + vm.data.model[vm.camelCased(key)]; }
} }
} axios.get(url).then(function (response) {
axios.get(url).then(function (response) { if (response.data !== true) {
vm.removeError(name); vm.updateError(name, 'error');
}
if (response.data !== true) { else {
var message = response.data === false ? property.remote.message : response.data; vm.removeError(name,'loading');
vm.updateError(name, message); }
} vm.removeError(name,'loading');
}).catch(function (error) { }).catch(function () {
vm.removeError(name); vm.updateError(name, 'error');
vm.updateError(name, 'network error'); });
}); return false;
return false; },'loading');
}, 'loading'); }
console.log(this.data.errors); },
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) { validInternal: function (validator, name, valid, message) {
if (!validator) { if (!validator) {
return; return;
} }
var property = this.data.schema.properties[name]; message = message || validator.message;
if (valid()) { if (valid()) {
this.removeError(name); this.removeError(name,message);
} }
else { else {
message = message || validator.message;
this.updateError(name, message); this.updateError(name, message);
} }
}, },
@ -153,8 +161,13 @@
removeError: function (name, message) { removeError: function (name, message) {
var error = Enumerable.from(this.data.errors).firstOrDefault(o => o.key.toLowerCase() === name.toLowerCase()); var error = Enumerable.from(this.data.errors).firstOrDefault(o => o.key.toLowerCase() === name.toLowerCase());
if (error) { if (error) {
var index = _.findIndex(this.data.errors, o => o.key.toLowerCase() === name.toLowerCase()); if (Enumerable.from(error.value.errors).any(o => o.errorMessage === message)) {
this.data.errors.splice(index, 1); 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) { 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) { getValidationSummary: function (excludePropertyErrors) {
var query = Enumerable.from(this.data.errors); var query = Enumerable.from(this.data.errors);
if (excludePropertyErrors) { if (excludePropertyErrors) {
@ -215,20 +221,24 @@
return key !== 'id'; return key !== 'id';
}, },
onSubmit: function (e) { onSubmit: function (e) {
if (this.valid()) { var vm = this;
var vm = this; new Promise(function (resolve, reject) {
axios.post(e.target.action, this.data.model).then(function (response) { vm.valid();
if (response.status === 204) { }).then(function () {
var url = '/router/shared/list.html?entity=' + vm.entity; if (vm.data.errors.length === 0) {
setTimeout(function () { axios.post(e.target.action, vm.data.model).then(function (response) {
router.push(url); if (response.status === 204) {
}, 1000); var url = '/router/shared/list.html?entity=' + vm.entity;
} setTimeout(function () {
else { router.push(url);
vm.data = response.data; }, 1000);
} }
}); else {
} vm.data = response.data;
}
});
}
});
} }
} }
} }

Loading…
Cancel
Save