Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Baby steps to Backbone - step 2 - Validation
var Feedback = Backbone.Model.extend({
url: '/feedback',
defaults: {
'email': '',
'website': '',
'feedback': ''
validate: function (attrs) {
var errors = [];
if (! {
errors.push({name: 'email', message: 'Please fill email field.'});
if (! {
errors.push({name: 'feedback', message: 'Please fill feedback field.'});
return errors.length > 0 ? errors : false;
$(function () {
var model = new Feedback();
var view = new FeedbackFormView ({model: model});
var FeedbackFormView = Backbone.View.extend({
className: 'row',
template: '\
<legend>Share the feedback</legend>\
<div class="control-group email">\
<input type="text" id="email" placeholder="Your email address...">\
<span class="help-inline"></span>\
<div class="control-group website">\
<label>Web site</label>\
<input type="text" id="website" placeholder="Your website...">\
<span class="help-inline"></span>\
<div class="control-group feedback">\
<textarea id="feedback" class="input-xxlarge" placeholder="Feedback text..." rows="6"></textarea>\
<span class="help-inline"></span>\
<button type="submit" id="submit" class="btn">Submit</button>\
events: {
'click #submit': 'submitClicked'
render: function () {
return this;
submitClicked: function (e) {
var me = this;
var options = {
success: function () {
error: function (model, errors) {
var feedback = {
email: this.$('#email').val(),
website: this.$('#website').val(),
feedback: this.$('#feedback').val()
};, options);
showErrors: function(errors) {
_.each(errors, function (error) {
var controlGroup = this.$('.' +;
}, this);
hideErrors: function () {
Copy link

phillipconrad commented Apr 25, 2014

This example doesn't work with the current backbone version 1.1.2.
The _validate-method triggers the event 'invalid' instead of calling 'options.error'.

    // Run validation against the next complete set of model attributes,
    // returning `true` if all is well. Otherwise, fire an `"invalid"` event.
    _validate: function(attrs, options) {
      if (!options.validate || !this.validate) return true;
      attrs = _.extend({}, this.attributes, attrs);
      var error = this.validationError = this.validate(attrs, options) || null;
      if (!error) return true;
      this.trigger('invalid', this, error, _.extend(options, {validationError: error}));
      return false;

Copy link

Saneesh commented Jul 8, 2014

So how can we make it work?

Copy link

bugbuilder commented Jul 13, 2014



Copy link

ghost commented Nov 13, 2014

Where I need to put this code listenTo in the feedback app, I'm new to this technology, this one is not working with the current backbone version, can you please tell me where to put this new code I mean in which file? Thank you so much...

Copy link

cheshireoctopus commented Jan 14, 2015

@shravanaz86 - a bit late to the party, but believe you could do the following:

this.model.on('invalid', function (model, error) {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment