Knockout-Contrib / Knockout-Contrib/Knockout-Validation

Error messages from data-val-xxx attributes

Open
#302 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
1k
Forks
366
PR merge metrics
No merged PRs in 30d

Description

I'm using knockout validation on a form that was generated by ASP.Net MVC with unobtrusive jQuery validation turned on. So on form controls I'm getting data-val attributes which include an error string from a resource file.

So for example, if I have a string property and I render it using @Html.TextBoxFor(...) and the string is a required property, I'll get data validation attributes like so:

Note that the required attribute on the property has the ErrorMessageName and ErrorMessageType properties set so that the error message is retrieved from resource files on the server based on the current culture.

So my question is, is there a way to get knockout validation to use the data-val error messages rather than the included ones.

My first attempt has been to try to create an additional binding and in the init function find all the data-val-xxx attributes and get their value, then use the xxx part to try to find the right validator and set its error message. Unfortunately I'm having some trouble with this approach so far. Can't seem to get a collection of all the data-val-xxx attributes and their values.

My motivation for this was to avoid having our translators have to worry about javascript files in addition to all the resx files they are already translating. The resx files contain all the messages we need, and unobtrusive jQuery validation puts them right on the page, we just have to get them to the knockout validators.

Thanks for your time if you've made it this far :)

Contributor guide

Open the contributing guide

Research direction

Start with the additional binding described in the issue and inspect how the knockout validators currently obtain their error messages. Investigate the data-val-xxx attributes rendered by ASP.NET MVC and how their names correspond to validators. Done means server-rendered, culture-specific messages are used instead of the included knockout validation messages.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
frontend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.