Manish Sapkal
Manish Sapkal

Reputation: 6241

backbone validate single attribute in save only

I have one model with 2 attributes. Let me explain first.

Backbone.Model.extend({
    validation: {
            username: [
               {required: true, msg: 'Enter email/username.'},
            ],   
            password: [
               {required: true,msg: 'Enter password.'}, 
               {minLength:20,msg: 'Incorrect password length.'}]
        },
}); 

I want validate single attribute in save function. Do you have any idea? means, If my username & password field is blank, then error should be occurred for username only.

I am using backbone.validation with backbone.

Thanks

Upvotes: 2

Views: 1752

Answers (3)

jonnybot
jonnybot

Reputation: 2453

There are two approaches you might use:

Approach 1

I think the easiest way to do this would be that you don't set the password field until after the username is validated. To quote from the FAQ:

What gets validated when?

If you are using Backbone v0.9.1 or later, all attributes in a model will be validated. However, if for instance name never has been set (either explicitly or with a default value) that attribute will not be validated before it gets set.

This is very useful when validating forms as they are populated, since you don't want to alert the user about errors in input not yet entered.

If you need to validate entire model (both attributes that has been set or not) you can call validate() or isValid(true) on the model.

So, don't call validate on your whole model. Call it on the username field first, then the password field.

Also, don't set the password field in your model until after the username has been validated.

Approach 2

Another approach would be to use the conditional validation described in the FAQ:

Do you support conditional validation?

Yes, well, sort of. You can have conditional validation by specifying the required validator as a function.

So, your code might look something like:

Backbone.Model.extend({
    validation: {
            username: [
               {required: true, msg: 'Enter email/username.'},
            ],   
            password: [
               {required: function(val, attr, username) {
                   return Bool(username); //some code here- return true if username is set, false if it is not. This rough example may not work in the real world.
               },msg: 'Enter password.'}, 
               {minLength:20,msg: 'Incorrect password length.'}]
        },
}); 

I'm pretty sure that's what Ulugbek Komilovich is suggesting, though I'm not sure the syntax in that answer is quite correct.

Upvotes: 1

Ulug'bek
Ulug'bek

Reputation: 2832

M = Backbone.Model.extend({
    validation: {
      username: [
        {required: true, msg: 'Enter email/username.'},
      ],   
      password: function(value, attr, computedState) {
        // new M(this.get('username')); //or use second way
        if(this.get('username')) {
          return 'Enter email/username.';
        }
        // other checks
      }
    },
}); 

Upvotes: 0

TYRONEMICHAEL
TYRONEMICHAEL

Reputation: 4244

To validate a single or multiple properties, using Backbone.Validation, use the following code:

yourModel.isValid('username') // or
yourModel.isValid([ 'attribute1', 'attribute2' ])

Upvotes: 3

Related Questions