0
votes

I am having a textbox binded with datepicker and i am applying knockout validation to it .

I need some clarification on how knockout does the Job of validation like for the textbox i have Extended required field validation first and some custom validation next to it .

My major concern is if knockout executes validation conditions even after 1st condition fails(required in mycase) then i got a problem i.e i get NULL exceptions .

Let me put this in example :

    self.startDate.extend({required: true}),

    self.startDate.extend({
 // here i have some complex logic where i `split` date `(like self.startDate().split('/') )`  date and do the following .
    })

Onload my textbox will be empty with no date . is suppose complete validation check is done onload i get ERROR at split as self.startDate().split('/') where split is undefined due to no data in observable .

Things i need clarity on are :

  • How validation check is done on load ?
  • If onload complete validation check is done means anyway we can make Validations work in a sequential way i.e once 1st condition is done it should move to check next like that

Any suggestion are much appreciated .

1
sadly no sWWW i am just looking for a case where will second validation condition executes even though condition one fails . i am hoping its like a if/else rather if - super cool
Typically there should be no two self.startDate with extend i need to make it in one set i am unable to do so . - super cool
So are you unable to have something like self.startDate({require: true, extender2: doSomething}) - sWW
well i haven't tried that exactly but it was one in my whilst and most important thing is as you mentioned above in comment self.startDate({require: true, extender2: doSomething}) will extender2 does even excecute if require:true fails . - super cool

1 Answers

0
votes

finally got what excatly i am looking for but in a complex and clumsy way which i want to avoid everytime

My code :

self.Validation = ko.validatedObservable([
                self.ForecastDateVM.extend({

required: { " enter something " },

validation: {validator: function (val, someOtherVal) {
                            if (val != undefined && someOtherVal() != undefined) {
                                var date1 = val.split('/'); 
                                var date2 = someOtherVal().split('/');

                            },
                            message: 'Must be greater ActualDate',
                            params: self.ActualDateVM
                        }
                    })
            ]);

Things i got myself clarified by severe debugging stuff :-

  • Onload whatever we have inside .extend({ those all gets checked up in a sequential success order i.e if 1st condtion fails it wont check for 2nd inside extend .

  • Point one made my work looks straight forward .

In short Knockout Validation works in if/else way .