Question

I have the following issues with my knockout model validations and not sure how to resolve them. Following is my model first of all, with the validation rules:

var Data = function (data) {
this.Val = data;
}

function ViewModel(item) {
var parse = JSON.parse(item.d);
var self = this;
this.Name = ko.observable(parse.Name);
this.UserType = ko.observable(parse.UserType);
this.ID = ko.observable(parse.ID).extend({ required: { params: true, message: "ID is    required" }, decimal: { params: 2, message: "Should be decimal"} });
this.Username = ko.observable(parsed.Username).extend({ required: {
        onlyIf: function () {
            return self.UserType() > 1;
        }
    }   
});

this.WeeklyData = ko.observableArray([]);
var records = $.map(parse.WeeklyData, function (data) { return new Data(data) });
this.WeeklyData(records);
this.WeeklyData2 = ko.observableArray([]);
var records = $.map(parse.WeeklyData2, function (data) { return new Data(data) });
this.WeeklyData2(records);
}

ko.extenders.numeric = function (target, precision) {
var result = ko.dependentObservable({
    read: function () {
        return target().toFixed(precision);
    },
    write: target
});

result.raw = target;
return result;
};

Here are my problems:

1) with the ID() observable, I want to restrict it to two decimal points, so I've created the validation extender 'numeric' but it's not working. Is there anything wrong with how I'm using it and how to correct it?

2) Also, if I want to restrict an observable to whole numbers, how can I do that?

3) when I define a rule with a condition, (i.e. Username()), how do I define a custom message for that? I was able to do it for default rules, but with the conditional rules, it's not working

4) I have two observable arrays WeeklyData1 and WeeklyData2 both of which contains Data() objects. I want to have separate min/max rules for these two, for example, min/max - 1,7 for WeeklyData1 and min/max - 1,150 for WeeklyData2. How can I get it done?

4) Right now my error messages appear right next to the data field, but I want all those to appear in a single validation summary, while displaying '*' against the field. I've been told to use Validation-bindings, but I'm not sure how to use it, can someone please give an example?

It's a lot of questions, I know, but I appreciate if someone could help.

Thanks in advance

Was it helpful?

Solution

Instead of diving in your code i have created a small-small demonstrations for your questions. Ok so here we go,

1) with the ID() observable, I want to restrict it to two decimal points.... and 2) Also, if I want to restrict an observable to whole numbers....

Your 1 and 2 question are pretty similar so i covered both of this in a single fiddle. Check this fiddle.

3) when I define a rule with a condition, (i.e. Username()), how do I define a custom message ....

You can use message property to set custom messages, Check this fiddle.

4) I have two observable arrays WeeklyData1 and WeeklyData2 both of which contains Data() objects

I am not clear which this question, what type of data both of these array contains and for what you want to set min/max rule ( array length or other ). So please clear this, than i will try to help on this.

5) Right now my error messages appear right next to the data field.....

This questions answer i already given in your how to? with knockout js validations question (Check update).

Let me know if it helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top