validation not working in knockout js

2019-09-06 20:39发布

问题:

I am validating an input field for unique parameters for this i am using this code .

self.loanterm = ko.observable().extend({ required: true,unique: {
                collection: self.termValues,
                externalValue: ""
                } });

but when i am adding duplicate value it doesn't showing me any message. termValues is an observable array whose values i am adding like this .

self.PopulateLoans = ko.computed(function(){
    ko.utils.arrayForEach(dummyData, function(item){
        self.Terms.push(new Term(item));
        self.termValues.push(item.loanterm);
    });
});

i am using knockout validation plugin for this.

Here is js fiddle link

回答1:

You are missing to add validationElement binding on your input fields

<input type="text" class="edit" data-bind="value: loanterm, visible: $root.isItemEditing($data),validationElement:loanterm"  />
<strong class="read" data-bind="text: loanterm,  visible: !$root.isItemEditing($data)" ></strong>
<input type="text" class="edit" data-bind="value: termIdentifier, visible: $root.isItemEditing($data),validationElement:termIdentifier"  />