使用BreezeJS / KnockoutJS / Durandal验证输入数据

时间:2014-04-15 11:52:09

标签: asp.net html5 knockout.js breeze durandal

我尝试使用BreezeJS / KnockoutJS / Durandal从表单中验证我的输入,但我没有得到任何结果。

这是我在观点中的输入:

<input data-bind="value:app_name, attr: {placeholder: resources.brand_create_placeholder_brand_name }" type="text" class="form-control input-sm" name="app_name" id="app_name" />

这是我的viewmodel:

define(['durandal/app', 'services/logger', 'plugins/router', 'knockout', 'breeze', 'toastr', 'services/resources'],
    function (app, logger, router, ko, breeze, toastr, resourcesSVC) {

    var vm = {

        app_name: ko.observable(), //.extend({ required: true, minLength: 30, }),
        ...,
        currency: ko.observable(),
        currencies: [{ name: "Euro", value: "EUR", synbol: "€" }, { name: "Dollar", value: "USD", synbol: "$" }, { name: "Pounds Sterling", value: "GBP", synbol: "£" }],
        use_smtp: ko.observable(false),

        activate: activate,
        title: "Create Brand",
        createEntity: createEntity,
        goBack: goBack,

        resources: {}
        }
    };

我在这里初始化我的服务:

var serviceName = 'breeze';
var manager = new breeze.EntityManager(serviceName);

在这里,我创建了我的实体:

function createEntity() {

    var DT = breeze.DataType;
    var Validator = breeze.Validator;

    var brand = manager.metadataStore.getEntityType("app");

    /*app_name*/
    var propAppName = brand.getProperty('app_name');
    brand.addValidator(Validator.required(), propAppName);
    brand.addValidator(Validator.maxLength({ maxLength: 30 }), propAppName);

    var newBrand = brand.createEntity('app');

    newBrand.app_name = vm.app_name();
    ...
    manager.addEntity(newBrand);
    manager.saveChanges()
        .then(createSucceeded)
        .fail(createFailed);

    function createSucceeded(data) {
        toastr.success("Brand created");
        app.trigger('brand:changed');
        router.navigate('#brands');
    }

    function createFailed(error) {
        var msgError = "Create failed: " + getErrorMessages(error);
        toastr.error(msgError);
        manager.rejectChanges();
    }
}

你能帮我设置每个领域的验证员吗?为每个值不正确的人显示自定义消息?

谢谢!

0 个答案:

没有答案