Former-commit-id: 0274c91ea78c3413f65b0c63363a70766783d76a
Former-commit-id: f51a8116a5a85e1ab539ec8351b7a434c24f0f20
TSXN
wanggang 5 years ago
parent ed84e0bfa4
commit 8d36c4c8b5

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

@ -89,15 +89,13 @@
});
},
change: function (name) {
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];
var list = [];
_.forIn(property.rules, function (value, key) {
var promise = value.valid(key, value, model)
});
list.push(
this.validInternal(property.required, name, o => Promise.resolve(value))
);
@ -165,7 +163,7 @@
var vm = this;
var list = [];
_.forIn(this.data.schema.properties, function (value, key) {
list.push(vm.changeInternal(key, true));
list.push(vm.change(key));
});
return Promise.all(list);
},

@ -0,0 +1,91 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<script>
var validator = {
errors: [],
valid: function (schema, model) {
this.errors = [];
var list = [];
for (var propName in schema.properties) {
var prop = schema.properties[propName];
for (var ruleIndex in prop.rules) {
var rule = prop.rules[ruleIndex];
list.push(this[rule.name](model, propName, rule));
}
}
return Promise.all(list);
},
addError: function (propName, name, message) {
var prop = this.errors.find(o => o.name === propName);
if (!prop) {
prop = { name: propName, errors: [] };
this.errors.push(prop);
}
var error = prop.errors.find(o => o.name === name);
if (!error) {
error = { name: name };
prop.errors.push(error);
}
error.message = message;
},
removeError: function (propName, name) {
var propIndex = this.errors.findIndex(o => o.name === propName);
if (propIndex > -1) {
var prop = this.errors[propIndex];
var errorIndex = prop.errors.findIndex(o => o.name === name);
if (errorIndex > -1) {
prop.errors.splice(errorIndex, 1);
if (prop.errors.length === 0) {
this.errors.splice(propIndex, 1);
}
}
}
},
required: function (model, propName, rule) {
var result = model[propName] ? true : false;
if (result) {
this.removeError(propName, rule.name, rule.message);
}
else {
this.addError(propName, rule.name);
}
return Promise.resolve(result);
}
};
var schema = {
type: 'object',
title: '对象',
properties: {
name: {
title: '名称',
rules: [
{
name: 'required',
message: '必填项'
}
]
}
}
};
var model = {
name: null
};
function valid() {
validator.valid(schema, model).then(function (value) {
alert(validator.errors.length);
model.name = 1;
}).then(function (value) {
validator.valid(schema, model).then(function (value) {
alert(validator.errors.length);
});
});
}
valid();
</script>
</body>
</html>
Loading…
Cancel
Save