Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6744347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:04:22+00:00 2026-05-26T12:04:22+00:00

It is easy to bind to the ‘error’ event of an existing model, but

  • 0

It is easy to bind to the ‘error’ event of an existing model, but what is the best way to determine if a new model is valid?

Car = Backbone.Model.extend({
  validate: function(attributes) {
    if(attributes.weight == null || attributes.weight <=0) {
      return 'Weight must be a non-negative integer';
    }
    return '';
  }
});

Cars = Backbone.Collection.extend({
  model: Car
});

var cars = new Cars();
cars.add({'weight': -5}); //Invalid model. How do I capture the error from the validate function?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T12:04:23+00:00Added an answer on May 26, 2026 at 12:04 pm

    Validation logic can be triggered explicitly by calling the validate method of your model. This will not, however, cause an error event to be triggered. You can trigger an error event manually for a model by calling the trigger method.

    One way to achieve your desired behavior is to manually trigger the event in your initialization method:

    Car = Backbone.Model.extend({
      initialize: function () {
        Backbone.Model.prototype.initialize.apply(this, arguments);
        var error = this.validate(this.attributes);
        if (error) {
          this.trigger('error', this, error);
        }
      },
      validate: function(attributes) {
        if(attributes.weight == null || attributes.weight <=0) {
          return 'Weight must be a non-negative integer';
        }
        return '';
      }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any easy way to bind to the ToString() method in a DataTemplate?
this is most likely very easy but i'm struggling $(window).bind(load resize, function(){ var wH
Is there any easy way (or no so easy ) to bind wpf combo
It's easy to bind something to the SelectedIndex of the ListBox, but I want
Is there an easy way to tell knockout to bind the selected value of
An easy problem, but for some reason I just can't figure this out today.
Sounds easy, but I've got a bug and I'm not sure what's causing it?
Probably easy one. How can i change rows or column heigh? xlApp = new
I have very easy JavaScript/CoffeSscript knowledge. I'm wondering if there's a better way to
Problem: Need to bind a strongly typed model which has a Gender as enum

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.