【问题标题】:Validate breeze complex-type without validate the entire entity验证微风复杂类型而不验证整个实体
【发布时间】: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


    【解决方案1】:

    没有单独的方法来验证复杂类型,因为验证结果都是“父”实体的一部分。复杂类型属性被视为实体的一部分,而不是独立实体。

    您可以对父实体的“复杂”属性调用 validateProperty。

    【讨论】:

    • 我试过了,即使 complexType 无效,它也会返回“true”。它没有检查它的每一个属性。我还尝试对 complexType 的每个属性都使用“validateProperty”,但是它没有给我验证错误,它只是告诉我该属性无效。
    • 我根据您的回答编辑了我的问题。你能再读一遍然后回答我吗?
    • 我找到了方法。谢谢
    猜你喜欢
    • 2014-03-01
    • 1970-01-01
    • 2021-07-24
    • 2021-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多