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 8609727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:56:57+00:00 2026-06-12T03:56:57+00:00

I have created an object: var Person = Backbone.Model.extend({ defaults: { ‘name’: ”, ‘age’:

  • 0

I have created an object:

var Person = Backbone.Model.extend({
  defaults: {
    'name': '',
    'age': 0
  },
  initialize: function() {
    this.on('error', function(model, error){
      console.log(error);
    });
  },
  validate: function(attributes){
    if (attributes.name == '') {
      return "How do we call him!?";
    }
  },
  changeName: function(name) {
    this.set({'name':name});
  },
  getOlder: function() {
    this.set({'age': this.get('age') +1});
  }   
});

I create an instance of Person and put in a blank on the name and I never received an error. But when I set a blank name on an already created instance, it fired the validation.

var teejay = new Person;
teejay.changeName('');
=> How do we call him!?
teejay.get('name');
=> ""

From what I am seeing from the Backbone source code, I see

this.set(attributes, {silent: true});

Is it right to assume that validation is only done whenever attributes are changed or set?

  • 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-06-12T03:56:59+00:00Added an answer on June 12, 2026 at 3:56 am

    Backbone will always call the validate method when you set or save an attribute with the exception of when you pass in a hash to the constructor or when the model instantiates with the default hash.

    e.g.

    var myperson = new Person({name: ''});
    

    Will NOT call the validate method since the constructor MUST return a new instance of the model.

    However,

    var myperson = new Person();
    myperson.set('name', '');
    

    Will.

    Your validate method will never print out anything — the return on validate is used to test whether or not it worked. If you return anything “truthy” validation will fail, but that’s it. It’s up to you to trigger the 'error' message or whatever else you want.

    What you’re seeing is exactly as should be expected.

    TL;DR:
    Your defaults hash will not ever be validated since it has to return a valid model.

    (Also, allow me to shill for my Backbone.Validator project now — don’t write your own!)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a model named person: var person = Backbone.Model.extend({ initialize: function(){ console.log('cool'); },
I have a Javascript object created as follows: var ccStatTracker = (function (){ ccmap:{
I have a object in F# as follows... type Person(?name : string) = let
Imagine I have some javascript object eg var person = {}; and I am
I have this code: var Person = { name: , changes: { name to:
I have created this Update method public void Update(Person updated) { var oldProperties =
Currently I have this code var App = Ember.Application.create(); App.user = Ember.Object.create({ people: customers
So I have an object of ArrayProxy kind var App = Ember.Application.create(); App.car =
I have created an object at runtime by using innerHTML tag, now I want
Let me first describe my goal: I have created an object with 3 properties:

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.