如何使用Marionette.js的Backbone.Validation插件

时间:2014-01-14 15:32:14

标签: backbone.js twitter-bootstrap-3 marionette backbone.validation.js

我在我的Marionette.js应用程序中使用Twitter Bootstrap3和Backone.Validation(http://thedersen.com/projects/backbone-validation/)插件,但有些人怎么说都不能正确。 任何人都可以举例说明如何使用Marionette.js的Backbone.Validation (示例类似于http://thedersen.com/projects/backbone-validation/#examples和_http://jsfiddle.net/thedersen/c3kK2/)

更新: 我有以下编码,我预计一旦提交表单就会触发验证,但遗憾的是没有发生任何事情,甚至没有错误。 在我的app.js(全球)

//Backbone.Validation
Backbone.Validation.configure({
    forceUpdate: true
});

_.extend(Backbone.Validation.callbacks, {
    valid: function (view, attr, selector) {
        var $el = view.$('[name=' + attr + ']'),
            $group = $el.closest('.form-group');

        $group.removeClass('has-error');
        $group.find('.help-block').html('').addClass('hidden');
    },
    invalid: function (view, attr, error, selector) {
        var $el = view.$('[name=' + attr + ']'),
            $group = $el.closest('.form-group');

        $group.addClass('has-error');
        $group.find('.help-block').html(error).removeClass('hidden');
    }
});

我的模板如下所示:

<script id="signup-form" type='text/template'>
        <form class="form-signin control-group">
            <table>
                <tr><th><h2 class="form-signin-heading">Please sign up</h2></th></tr>
                <tr><td><input type="text" class="form-control" name="username" placeholder="Email address"></td></tr>
                <tr><td><input type="password" class="form-control" name="password" placeholder="Password"></td></tr>
                <tr><td><button class="btn btn-success form-control js-submit">Sign up</button></td></tr>
            </table>
        </form>

我的模型看起来像这样:

Entities.User = Backbone.Model.extend({
    urlRoot: "signup",

    defaults: {           

    },

    idAttribute: "_id",

    validation: {
        email: {
            required: true,
            pattern: 'email'
        },
        password: {
            minLength: 8
        }
    },
    validate:true
});

我的观点看起来像这样

Show.SignupPanel = Marionette.ItemView.extend({

    template: "#signup-form",


    events: {
        'click button.js-submit': 'signupClicked'
    },

    signupClicked: function (e) {
        //stop the default action of <a> tag and page refresh
        e.preventDefault();
        var data = Backbone.Syphon.serialize(this);
      if(this.model.isValid(true)) 
            this.trigger("form:submit", data);
    },

    initialize: function () {
        // This hooks up the validation
        // See: http://thedersen.com/projects/backbone-validation/#using-form-model-validation/validation-binding
        Backbone.Validation.bind(this);
    },

    remove: function () {
        // Remove the validation binding
        // See: http://thedersen.com/projects/backbone-validation/#using-form-model-validation/unbinding
        Backbone.Validation.unbind(this);
        return Backbone.View.prototype.remove.apply(this, arguments);
    }

});

1 个答案:

答案 0 :(得分:2)