valid by promise

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

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

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

Loading…
Cancel
Save