How to use Backbone.Validation plugin with Marionette.js

5k Views Asked by At

I am using Twitter Bootstrap3 with Backone.Validation(http://thedersen.com/projects/backbone-validation/) plugin in my Marionette.js app, but some how cannot get it right at all. can any one please give a simple example on how to use Backbone.Validation with Marionette.js (example similar to http://thedersen.com/projects/backbone-validation/#examples and _http://jsfiddle.net/thedersen/c3kK2/)

UPDATE: I have the following coding in place, I expected that the validation will be fired once the form is submitted, but unfortunately nothing is happening and there is not even an error. in my app.js (global)

//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');
    }
});

My template looks like this:

<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>

My model looks like this:

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

    defaults: {           

    },

    idAttribute: "_id",

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

My view looks like this

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

There are 1 best solutions below

0
On