0

Hi I have created a function object that contains a set of properties.This is what I have:

function LoginModelDTO(data) {
    var self = this;

    self.UserName = ko.observable(data.UserName).extend({
        minLength: {
            params: 25,
            message: "Username should have at least 25 chars"
        },
        required: {

            message: "Username is required"
        },
        maxLength: {
            params: 50,
            message: "Username should not have more then 50 chars"
        },
        trackChanges: null
    });
    self.Password = ko.observable(data.Password).extend({
        stringLength: {
            params: 25,

        },
        required: {

            message: "Password is required"
        },
        trackChanges: null
    });
    self.RememberMe = ko.observable(data.RememberMe).extend({
        trackChanges: null
    });

self.isValid = ko.computed(function () {
        var bool = self.FirstName.isValid() &&
            self.Username.isValid() &&
            self.Password.isValid() &&
            self.RememberMe() &&
        return bool;
    });
}

What I would like is to be able to find a way to iterate over each property and ask if it's valid without writing every property every time because I also have to write a similar structure like self.isValid for hasChanges , revertChanges etc.

Furthermore I will need to create other similar objects to LoginModelDTO that have around 30-35 properties.This will result in alot of code and a bigger javascript file then needed.

Is there any way I can iterate only threw the properties and check if they are valid? isValid should be skipped

M Falanga
  • 1,897
  • 17
  • 21
aleczandru
  • 5,319
  • 15
  • 62
  • 112
  • 1
    `for (propertyName in self) { // check for self[propertyName].isValid() }` and skip the ones you don't want to check? – eis Jun 18 '13 at 15:59
  • Possible duplicate - http://stackoverflow.com/questions/684672/loop-through-javascript-object/684692#684692 – Ian Jun 18 '13 at 16:00
  • in case you don't want to reinvent the wheel http://jqueryvalidation.org – mcgrailm Jun 18 '13 at 16:01
  • 1
    I am using knockout for validation but a, extending it's functionality because of the requirments – aleczandru Jun 18 '13 at 16:02
  • @mcgrailm you know, sometimes is better so reinvent the wheel. If you dont, you will not know how does it works and HOW TO create one can you dont have one. – Misters Jun 18 '13 at 16:03
  • all good, just wanted to make sure the OP knew there was something already exists :) – mcgrailm Jun 18 '13 at 16:05

3 Answers3

1

WELL for..in statement can help you:

var obj = {

pro1:"hello",

pro2:function(){
//code here
},
etc:function(){

}//...
}

for(var property in obj)
{
if(obj.hasOwnProperty(property))
{
console.log(property)//display the name of the property and of course the access
}
}

And to access to the values of the property you can do this:

for(var property in obj)
{
if(obj.hasOwnProperty(property))
{
console.log(obj[property])//display the value of the property(in case that you need it)
}
}
Misters
  • 1,337
  • 2
  • 16
  • 29
1

eis gave you part of it in the comments, and Misters gave you a part in the answer, but here's it all together:

var allValidatablesAreValid = true;
for (var property in self)
{
    if (self.hasOwnProperty(property) && self[property]["isValid"]) {
        allValidatablesAreValid = allValidatablesAreValid && self[property].isValid();
    }
    // You can add an early bail out here:
    // if (!allValidatablesAreValid) { break; }
}
Scott Mermelstein
  • 15,174
  • 4
  • 48
  • 76
0

Since the question was related to the knockout-validation library, I thought I would show how to do this using the library, itself.

self.isValid = ko.computed(function () {
    return ko.validatedObservable(self).isValid()
});

See the above link for more information.

M Falanga
  • 1,897
  • 17
  • 21