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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:52:23+00:00 2026-06-17T15:52:23+00:00

My validate function is not getting called in the code below:- var Vehicle =

  • 0

My validate function is not getting called in the code below:-

var Vehicle = Backbone.Model.extend({
    color: 'green',
    validate: function (attrs) {
        var validColors = ['white', 'red', 'blue', 'yellow'];
        var colorIsValid = function (attrs) {
            if (!attrs.color) return true;
            return _.contains(validColors, attrs.color);
        }
        if(!colorIsValid(attrs)) {
            return "color must be one of: " +validColors.join(",");
        }
    }
});

var car = new Vehicle();

car.on('error', function (model, error) {
    console.log(error);
});

console.log(car.get('color'));
car.set('color', 'muave');

Please see fiddle
http://jsfiddle.net/vineet85/Fa8jr/5/

Can someone tell me why the validate function is not getting called?

  • 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-17T15:52:24+00:00Added an answer on June 17, 2026 at 3:52 pm

    In Backbone.js validate is called automatically on save but not on set.

    If you want validations to run when setting a value you will need to use the validate option. e.g.

    car.set('color', 'muave', {validate: true});
    

    See http://backbonejs.org/#Model-validate

    The error event is triggered when an error occurs, typically on the server, when trying to save the object. See http://backbonejs.org/#Events-catalog

    If you want to catch validation failures try handling the invalid event:

    car.on('invalid', function (model, error) {
        console.log(error);
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Markup: <input type=text name=email /> Code: $(':text').focusout(function(){ $(this).validate(function(){ $(this).attr('name'); }); }); Plugin: (function($){ $.fn.validate
I can do this jQuery.fn.validate = function(options) { var defaults = { validateOPtions1 :
I used the following code: <script type=text/javascript> function validate() { if ((document.changepass.newpassword.value != '')
Using this code $.getJSON(http://mydomain.com/getcustomers?callback=?,function(data) { .... }).error(function(err){ .... }) I'm getting jQuery16103492487950357067_1336742910384 was not
I've some problems getting KO and jQuery Validate plugin work together. My model: Parameter
I'm using this jQuery validation plugin and getting the famous: $(#Form).validate is not a
I'm having trouble getting my simple contact form to validate and am not sure
I have two asp.net textboxes and need to put a validate function in javascript
It is has been suggested that it is best to initialize a $('#form').validate({}) function
I have done various tests and I have found that the jQuery validate function

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.