【发布时间】:2014-09-01 12:17:52
【问题描述】:
当你想验证你编写的微风实体时:
this.entityAspect.validateEntity()
但是,如果我只想为复杂类型触发验证,而不触发整个实体的验证呢?
complexType.complexAspect 没有方法 validateEntity。
那么,我该怎么办?
看到杰回答后编辑:
我尝试使用方法 validateProperty。 但结果是它总是返回 true,因为它没有检查每个属性。 因此,我尝试多次调用方法 validateProperty,每次都是针对 complexType 的其他字段。它给了我有效/无效的布尔结果,但不更新验证错误。
这是我看到杰回答后尝试的代码,但没有帮助:
validateSingleField(myComplexProertyName);
第一个版本的 validateSingleField 函数:(结果是它总是返回 true,因为它不会检查每个属性)
function validateSingleField(object, fieldName) {
var entityAspect = object.entityAspect;
var objectType = object.entityType;
var prop = objectType.getProperty(fieldName);
var value = object.getProperty(fieldName);
if (prop.validators.length > 0) {
var context = { entity: entityAspect.entity, property: prop, propertyName: fieldName };
if (entityAspect._validateProperty(value, context)) {
return true;
}
return false;
}
}
第二个版本:(它给了我有效/无效的布尔结果,但不更新验证错误。)
function validateSingleField(object, fieldName) {
var aspect = object.entityAspect || object.complexAspect;
var entityAspect = object.entityAspect || object.complexAspect.getEntityAspect();
var objectType = object.entityType || object.complexType;
var prop = objectType.getProperty(fieldName);
if (prop.isComplexProperty) {
var isOk;
objectType.getProperties().forEach(function (p) {
isOk = isOk && validateSingleField(object[fieldName](), p.name)//writing 'object[fieldName]()' - is for send the entire complexType of the entity
});
return isOk;
}
else {
{
var value = object.getProperty(fieldName);
if (prop.validators.length > 0) {
var context = { entity: entityAspect.entity, property: prop, propertyName: fieldName };
if (entityAspect._validateProperty(value, context)) {
return true;
}
return false;
}
}
}
}
【问题讨论】:
标签: javascript breeze