Note that there are some explanatory texts on larger screens.

plurals
  1. POknockout validation unique in list
    primarykey
    data
    text
    <p>I'm trying to validate an entry in a list to be unique from all other entries in the list using ko.validation, but I'm having issues with validation running when it shouldn't.</p> <p>I have an editable list (a <code>ko.observableArray</code>), and each item in that array is a view model with a <code>ko.observable</code> on it:</p> <pre><code>var vm = function (data) { var self = this; self.items = ko.observableArray(); _.each(data.words, function (word) { self.items.push(new listItemVm({parent: self, word: word.word})); }); }; var listItemVm = function (data) { var self = this; self.parent = data.parent; self.word = ko.observable(data.word); }; </code></pre> <p>Then I add some validation to <code>listItemVm.word</code> ko.observable. I want each one to be unique:</p> <pre><code>var listItemVm = function (data) { var self = this; self.parent = data.parent; self.word = ko.observable(data.word).extend({ validation: { validator: function (name, params) { console.log("validating " + name); // word we are editing must be different from all other words // uncommenting this next line causes the behaviour // I would expect because params.parent.items() // is not called //return true; var allWords = params.parent.items(); // exclude current view model we are editing var otherWordViewModels = _.filter(allWords, function (row) { return row !== params.currentRow; }); var otherWords = _.map(otherWordViewModels, function (item) { return item.word(); }); return !_.contains(otherWords, name); }, message: 'Must be unique', params: { currentRow: self, parent: self.parent } } }); }; </code></pre> <p>I give it some data, and wrap it in some HTML: <a href="http://jsfiddle.net/9kw75/3/" rel="nofollow">http://jsfiddle.net/9kw75/3/</a></p> <p>Now, this does <em>work</em> - the validation runs correctly and shows invalid when the values of the two inputs are equal - but have a look in the console on that fiddle. Why does the validation routine run three five times on load, and why do both fields validate when just one value updates?</p> <ul> <li><strong>On page load</strong> <ul> <li><em>Expected</em>: validation runs once for each input field.</li> <li><em>Actual</em>: validation runs three times for one input, and twice for the other.</li> </ul></li> <li><strong>On value update (either input field)</strong> <ul> <li><em>Expected</em>: validation runs for altered input field only</li> <li><em>Actual</em>: validation runs for both input fields</li> </ul></li> </ul> <p>It's worth noting that this strange behaviour is only observed after reading <code>params.parent.items()</code> in the validator. If the return is commented out, the behaviour I would expect is observed.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload