valid by promise

Former-commit-id: b0f97efcbfb6c90e2820f7adb6978f844adb89c5
Former-commit-id: 27df0ba361385d42a89b4b2acdd90f5e6cc4a77a
TSXN
wanggang 5 years ago
parent b2ddc777a0
commit ed84e0bfa4

@ -23,8 +23,8 @@ namespace IoT.Shared.Areas.Admin.Controlls
public async Task<IActionResult> Valid(string name)
{
await Task.Delay(3000);
return Json(name=="123");
await Task.Delay(1000);
return Json(name == "123");
}
public override string GetNodeNumber(EditNodeModel model)

@ -89,71 +89,100 @@
});
},
change: function (name) {
this.changeInternal(name, false);
this.changeInternal(name, false).then(function () {
console.log(name + ' valid complete');
});
},
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);
var list = [];
list.push(
this.validInternal(property.required, name, o => Promise.resolve(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()));
this.validInternal(property.number, name, o => /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/.test(value));
this.validInternal(property.digits, name, o => /^\d+$/.test(value));
//dateiso
list.push(
this.validInternal(property.email, name, o => Promise.resolve(/^[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)))
);
list.push(
this.validInternal(property.url, name, o => Promise.resolve(/^(?:(?:(?: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)))
);
list.push(
this.validInternal(property.date, name, o => Promise.resolve(!/Invalid|NaN/.test(new Date(value).toString())))
);
list.push(
this.validInternal(property.number, name, o => Promise.resolve(/^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/.test(value)))
);
list.push(
this.validInternal(property.digits, name, o => Promise.resolve(/^\d+$/.test(value)))
);
//dateiso
//creditcard
//accept
this.validInternal(property.pattern, name, o => new RegExp(property.pattern.regex).test(value));
this.validInternal(property.minLength, name, o => value.length >= property.minLength.min);
this.validInternal(property.maxLength, name, o => value.length <= property.maxLength.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.equalTo, name, o => o === vm.data.model[vm.camelCased(name)]);
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)];
list.push(
this.validInternal(property.pattern, name, o => Promise.resolve(new RegExp(property.pattern.regex).test(value)))
);
list.push(
this.validInternal(property.minLength, name, o => Promise.resolve(value.length >= property.minLength.min))
);
list.push(
this.validInternal(property.maxLength, name, o => Promise.resolve(value.length <= property.maxLength.max))
);
list.push(
this.validInternal(property.rangelength, name, o => Promise.resolve(value.length >= property.length.min && value.length <= property.length.max))
);
list.push(
this.validInternal(property.range, name, o => Promise.resolve(parseFloat(value) >= property.range.min && parseFloat(value) <= property.range.max))
);
list.push(this.validInternal(property.equalTo, name, o => Promise.resolve(o === vm.data.model[vm.camelCased(name)]))
);
list.push(
this.validInternal(property.remote, name, o => new Promise((resolve, reject) => {
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) {
if (response.data !== true) {
axios.get(url).then(function (response) {
if (response.data !== true) {
vm.updateError(name, 'error');
}
resolve(response.data === true);
}).catch(function () {
vm.updateError(name, 'error');
}
else {
vm.removeError(name,'loading');
}
vm.removeError(name,'loading');
}).catch(function () {
vm.updateError(name, 'error');
});
return false;
},'loading');
resolve(false);
});
}), 'error')
);
}
return Promise.all(list);
},
valid: function () {
var vm = this
var vm = this;
var list = [];
_.forIn(this.data.schema.properties, function (value, key) {
vm.changeInternal(key,true);
list.push(vm.changeInternal(key, true));
});
return this.data.errors.length === 0;
return Promise.all(list);
},
validInternal: function (validator, name, valid, message) {
if (!validator) {
return;
}
message = message || validator.message;
if (valid()) {
this.removeError(name,message);
}
else {
this.updateError(name, message);
if (validator) {
var vm = this;
message = message || validator.message;
return valid().then(function (value) {
if (value) {
vm.removeError(name, message);
}
else {
vm.updateError(name, message);
}
});
}
return Promise.resolve('');
},
camelCased: function (name) {
return name[0].toLowerCase() + name.substr(1);
@ -222,9 +251,7 @@
},
onSubmit: function (e) {
var vm = this;
new Promise(function (resolve, reject) {
vm.valid();
}).then(function () {
vm.valid().then(function (value) {
if (vm.data.errors.length === 0) {
axios.post(e.target.action, vm.data.model).then(function (response) {
if (response.status === 204) {

Loading…
Cancel
Save