Knockout-Validation icon indicating copy to clipboard operation
Knockout-Validation copied to clipboard

adding validation to array items

Open cstavro opened this issue 14 years ago • 7 comments

I've been working on adding validation to new items dynamically added to an array of items and I'm having a bit of an issue.

http://jsfiddle.net/gFzQE/

In the fiddle you'll see 3 buttons: addWorks adds an item and adds the validation before pushing it into the array

addFails pushes the item into the array and then adds validation

addFails2 pushes the item into the array and then loops through all items in the array to add validation

Am I doing something wrong with these 2 failing methods by trying to add validation after it's been added to an observable array or is this a bug?

cstavro avatar Feb 06 '12 20:02 cstavro

@cstavro

Interesting use-case. In your 'fails' methods, the items are getting the correct validation logic added. However since you are updating the 'items' observableArray first, this is triggering the 'foreach' binding, and thus also the 'value' binding (which is where we insert validation messages, etc...).

So, basically your UI is updating before the validation logic has been applied to your observables. A quick fix for this would be to always update your 'items' last.

http://jsfiddle.net/ZedPM/1/

I'm working on some modifications for v1.0... this might be a use-case I design for if it is common to patterns that you see.

ericmbarnard avatar Feb 06 '12 20:02 ericmbarnard

My example is somewhat simplified to demonstrate the actual problem and you are correct your workaround works, it is however awkward based on my intended use case.

I'm trying to find a way to manage my view model from a server side object and push it to the client via an ajax request along with a set of validation rules and have everything actually execute client-side. The JSON result going to the client uses the knockout.mapping library to convert everything to observables and then the validation is attached by way of the custom rules object. This current method works but like I said, it does seem a little fragile.

http://jsfiddle.net/VGQYt/1/

I'm basically trying to structure a 'rules' object which contains all the validation rules for my model. The applyRules method can then take the model and the rules and just mash them together appropriately.

If you look at the difference between the 'addItem' and 'addHack' methods here you'll see that I'm trying to add an item and then apply the rules to the entire viewModel.order to update it. That method doesn't actually work in that fiddle. The addHack method on the other hand does work but requires fairly intimate knowledge of the underlying model plus your suggested hack.

Is this something that could even be fixed in knockout.validation? Any other suggestions as to how to go about this?

cstavro avatar Feb 06 '12 22:02 cstavro

@cstavro

I can appreciate your need to build something a little more generic, and your use-case makes a lot more sense now.

Have you looked at using the 'validationMessage' binding? Instead of inserting Messages, maybe use this?

http://jsfiddle.net/PbrXw/1/

This could be addressed in ko.validation, however it requires a change to some core logic and I need to weigh how it might be accomplished along with the widespread need.

ericmbarnard avatar Feb 07 '12 02:02 ericmbarnard

We are wanting to do the exact same thing. Have you done something with 1.0 to accomodate this?

trygvelo avatar May 03 '12 08:05 trygvelo

I would like to do the same thing cstavro mentioned - use ko.mapping to dynamically update arrays and apply the rules to the new array items. Has any progress been is this area?

ghost avatar Apr 04 '13 07:04 ghost

I had the same problem and created a pull request for that: #223

delixfe avatar Apr 04 '13 07:04 delixfe

I am trying to use same viewmodel and change the validation rules dynamically. How can i do that?

ghost avatar Jun 20 '13 08:06 ghost