0

I am currently using asp.net mvc and knockoutjs in a project, where i need to be able to make the validation as specified in asp.net mvc view model (using data annotation) to be able to shown in knockout binding. The thing is the microsoft unobstructive validation does not work after knockout binding and I do not wish to redeclare the validation in knockout validation manually.

Is there any existing solution/simple workaround? since i think this is quite a common issue as it is easy to maintain validation with mvc data annotation

If not, i am thinking to serialising the json (which take the validation data-annotation into account) and bind it into ko.mapping.fromJS but i do not know how to start. Perhaps someone has experience on this.

Whatever it is, my expected outcome is to not rewrite the data annotation validation in mvc in any other place.

Johnny88
  • 43
  • 4
  • Can you provide a sample showing what is not working? I'm pretty sure knockout and jquery unobtrusive validation can work together – dotnetom Aug 21 '15 at 17:23
  • @dotnetom unfortunately nothing can be debug. there is no error message in developer mode of browser. Perhaps i shouldn rephrase my question as knockout does not work with data annotation validation in mvc instead of jquery unobstructive. Data annotation works well with jquery unobstructive but not when knockout is introduced – Johnny88 Aug 21 '15 at 18:21
  • @dotnetom . A similar question has been posted here [link](http://stackoverflow.com/questions/15297410/mvc-validation-lost-in-knockoutjs-post) and some other place in the forum but the answers did not fit my expectation. Most of the answer either point to writing validation with knockout validation or library that has not been updated and maintained for years. – Johnny88 Aug 21 '15 at 18:31
  • It is possible to use unobtrusive validation with knockout but there are restrictions. The fields that you are validation need to be static for it to work. Can you please provide an example as to what you are expecting to validate? – Wayne Ellery Aug 21 '15 at 23:41
  • @WayneEllery required field, regex etc. what do you mean by static here? – Johnny88 Aug 22 '15 at 06:21
  • I mean that the fields that are displayed when the page first loads are the ones that are there all the time. If for instance a fields shows and hides then it's difficult to validate using only unobtrusive validation. The easiest way to accomplish what you want would be just to move the validation to the server side and then perform ajax calls to validate. Requirements matter so without a specific example it's really impossible to provide a one fits all solution. – Wayne Ellery Aug 22 '15 at 06:24
  • @WayneEllery we need a simple client side validation generated from data annotation in mvc as well. what we trying to accomplish here is get the mvc data annotation attributes to dynamically convert to knockout js validation as what the microsoft unobstructive validation works – Johnny88 Aug 23 '15 at 15:26
  • I think you will need to write something to do that that. It has crossed my mind to do something like this in the past – Wayne Ellery Aug 23 '15 at 22:02
  • Are you returning the data via an AJAX query? If you are then you could extend the JSON response to include the validation metadata and then dynamically add the validation when you load your client viewmodel – Robert Slaney Aug 24 '15 at 02:26

0 Answers0