扩展实体的微风验证

时间:2014-02-04 22:37:32

标签: javascript knockout.js breeze

我似乎无法在Breeze中为扩展实体添加验证。这是我的代码。我试图验证年龄只接受数值。当我输入年龄和保存字母时,验证不会触发。

First Name <input data-bind="value: employee().firstName" />
Last Name <input data-bind="value: employee().lastName" />
Age <input data-bind="value: employee().age" />
<button data-bind="click: save">Save</button>
<script src="~/Scripts/knockout-3.0.0.js"></script>
<script src="~/Scripts/q.js"></script>
<script src="~/Scripts/breeze.debug.js"></script>
<script>
    breeze.NamingConvention.camelCase.setAsDefault();
    var
        em = new breeze.EntityManager("/breeze/EmployeeApp"),
        Employee = function () {
            this.age= ko.observable('');
        },
        vm = function () {
            var
                employee = ko.observable(''),
                employeeLoaded = function (data) {
                    employee(data.entity);
                },
                initialize = function () {
                    em.metadataStore.registerEntityTypeCtor("Employee", Employee);
                    em.fetchMetadata().then(function () { initValidation(em) }).fail(function (error) { alert(error.message); });
                    function initValidation(em) {                            
                        var e = em.metadataStore.getEntityType("Employee");
                        e.getProperty("age").validators.push(breeze.Validator.number());
                        em.fetchEntityByKey("Employee", 1).then(employeeLoaded).fail(function (error) { alert(error.message); });
                    }
                },
                save = function () {
                    return em.saveChanges();
                };
            return {
                employee: employee,
                initialize: initialize,
                save: save
            };
        }();
    vm.initialize();
    ko.applyBindings(vm);
</script>

1 个答案:

答案 0 :(得分:0)

我无法重现这个问题。这是一个传递的DocCode测试,它演示了未映射属性的验证,例如age属性:

test("validates an unmapped property", 3, function () {
    var em = newEm();
    var meta = em.metadataStore;

    // add unmapped property
    var employeeCtor = function () { this.foo = ko.observable(42); };
    meta.registerEntityTypeCtor("Employee", employeeCtor);

    var empType = em.metadataStore.getEntityType("Employee");
    var fooProp = empType.getProperty("foo");
    ok(fooProp.isUnmapped, "Employee.foo should be an unmapped property");

    // add "number" validator to unmapped Foo
    fooProp.validators.push(breeze.Validator.number());

    var emp = em.createEntity(empType);

    // create a DataType error for the 'foo' property
    emp.foo('this is a string');

    // get the first (and presumably only) 'foo' property error.
    var firstFooErr = emp.entityAspect.getValidationErrors('foo')[0];
    var emsg = firstFooErr && firstFooErr.errorMessage;

    ok(!!firstFooErr, "should have a 'foo' error after setting foo to a string"
    + (emsg ? " and its message is "+emsg : ""));

    var fooErrValidator = firstFooErr.validator;
    equal(fooErrValidator.name, 'number', "foo error Validator should be 'number'");

});