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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:14:08+00:00 2026-06-05T19:14:08+00:00

I need to validate a form with a bunch of inputs in it. And,

  • 0

I need to validate a form with a bunch of inputs in it. And, if an input is invalid, indicate visually in the form that a particular attribute is invalid. For this I need to validate each form element individually.

I have one model & one view representing the entire form. Now when I update an attribute:

this.model.set('name', this.$name.val())

the validate method on the model will be called.

But, in that method I am validating all the attributes, so when setting the attribute above, all others are also validated, and if any one is invalid, an error is returned. This means that even if my ‘name’ attribute is valid, I get errors for others.

So, how do I validate just one attribute?

I think that it is not possible to just validate one attribute via the validate() method. One solution is to not use the validate method, and instead validate every attribute on ‘change’ event. But then this would make a lot of change handlers. Is it the correct approach? What else can I do?

I also think that this points to a bigger issue in backbone:

Whenever you use model.set() to set an attribute on the model, your validation method is run and all attributes are validated. This seems counterintuitive as you just want that single attribute to be validated.

  • 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-05T19:14:09+00:00Added an answer on June 5, 2026 at 7:14 pm

    Validate is used to keep your model in a valid state, it won’t let you set an invalid value unless you pass a silent:true option.

    You could either set all your attributes in one go:

    var M=Backbone.Model.extend({
        defaults:{
            name:"",
            count:0
        },
    
        validate: function(attrs) {
            var invalid=[];
            if (attrs.name==="") invalid.push("name");
            if (attrs.count===0) invalid.push("count");
    
            if (invalid.length>0) return invalid;
        }
    });
    
    var obj=new M();
    obj.on("error",function(model,err) {
        console.log(err);
    });
    obj.set({
        name:"name",
        count:1
    });
    

    or validate them one by one before setting them

    var M=Backbone.Model.extend({
        defaults:{
            name:"",
            count:0
        },
    
        validate: function(attrs) {
            var invalid=[];
            if ( (_.has(attrs,"name"))&&(attrs.name==="") )
                invalid.push("name");
            if ( (_.has(attrs,"count"))&&(attrs.count===0) )
                invalid.push("count");
    
            if (invalid.length>0) return invalid;
        }
    });
    
    var obj=new M();
    obj.on("error",function(model,err) {
        console.log(err);
    });
    
    if (!obj.validate({name:"name"}))
        obj.set({name:"name"},{silent:true});
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to validate a bunch of form fields within one element. I just
I need to validate a form. This is some server side validation that checks
I need to validate two user input fields against each other in seam. Field1
I have a situation that if my form doesn't validate I need it to
I need to validate a form textfield that's bound to a property of a
I'm using jQuery.validity to validate PHP form inputs. I need to skip validation when
I need to validate a form. This form has some dropdowns and tinyMCE editor,
I have a form that uses the validation plugin, but I need to validate
I have a registration form on my website and need to validate the input
I need to validate an input on a form. I'm expecting the input to

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.