Search code examples
ruby-on-railsvalidationbackbone.jsclient-side-validation

Validation with backbone.js and Ruby on Rails


Does anyone have any advice or examples for doing validation with backbone.js and ruby on rails 2.x or 3.x?


Solution

  • I've been using the Backbone.validations plugin with great success. It allows you to define your validations just like you do in Rails models.

    var ValidatingModel = Backbone.Model.extend({
      validate : {
        name : {
          required  : true,
          pattern   : /[a-zA-Z]+/,
          minlength : 3,
          maxlength : 100
        },
        age : {
          type: "number",
          min: 0,
          max: 200
        },
        email : {
          type: "email"
        },
        homepage : {
          type: "url"
        },
        occupation : {
          in : [
            "Lawyer",
            "Doctor",
            "Professor",
            "Economist"
          ]
        }
      }
    });