Comments (2)
Thanks both for your kind words and for bringing this to my attention.
I'm not generally a user of EditorTemplates but I do want other people to be able to use them if that's their preferred approach. I was as equally baffled as you as to why this is happening. So much so that I ended up digging through the MVC source to try and understand it a little better. Unfortunately this didn't really reveal why it was working this way; rather just confirming that indeed it did work this way. Not terribly helpful...
My aim for the project is for the jQuery.Validation.Unobtrusive.Native helpers to mirror as closely as possible the out-of-the-box MVC helpers. Your example is evidence of us moving away from that and in the absence of a clearly better approach (I haven't come up with one) I'm keen to take some steps to move us back in line. To that end I've taken your approach and moved it into Mapper.GenerateHtmlWithoutMvcUnobtrusiveAttributes
as below:
public static IHtmlString GenerateHtmlWithoutMvcUnobtrusiveAttributes(
ModelMetadata metadata, TemplateInfo templateInfo,
Func<string, IHtmlString> htmlGenerator)
{
var cachedClientValidationEnabled = HtmlHelper.ClientValidationEnabled;
var cachedUnobtrusiveJavaScriptEnabled = HtmlHelper.UnobtrusiveJavaScriptEnabled;
HtmlHelper.ClientValidationEnabled = false;
HtmlHelper.UnobtrusiveJavaScriptEnabled = false;
// Clear the name if the HtmlFieldPrefix already contains it (typically only in EditorTemplates)
var name = metadata.PropertyName;
if (templateInfo.HtmlFieldPrefix.EndsWith(metadata.PropertyName))
name = name.Substring(0, name.LastIndexOf(metadata.PropertyName, StringComparison.InvariantCulture));
var html = htmlGenerator(name);
HtmlHelper.ClientValidationEnabled = cachedClientValidationEnabled;
HtmlHelper.UnobtrusiveJavaScriptEnabled = cachedUnobtrusiveJavaScriptEnabled;
return html;
}
Then in each of the Extensions files you can see the call into Mapper.GenerateHtmlWithoutMvcUnobtrusiveAttributes
has subtly changed:
TextBoxFor
// ....
var textBox = Mapper.GenerateHtmlWithoutMvcUnobtrusiveAttributes(metadata, htmlHelper.ViewContext.ViewData.TemplateInfo,
(propName) => htmlHelper.TextBox(propName, metadata.Model, format, attributes));
// ....
TextAreaFor
// ....
var textArea = Mapper.GenerateHtmlWithoutMvcUnobtrusiveAttributes(metadata, htmlHelper.ViewContext.ViewData.TemplateInfo,
(propName) => htmlHelper.TextArea(propName, metadata.Model as string, rows, columns, attributes));
// ....
RadioButtonFor
// ....
var radioButton = Mapper.GenerateHtmlWithoutMvcUnobtrusiveAttributes(metadata, htmlHelper.ViewContext.ViewData.TemplateInfo,
(propName) => htmlHelper.RadioButton(propName, value, attributes));
// ....
ListBoxFor
// ....
var listBox = Mapper.GenerateHtmlWithoutMvcUnobtrusiveAttributes(metadata, htmlHelper.ViewContext.ViewData.TemplateInfo,
(propName) => htmlHelper.ListBox(propName, selectList, attributes));
// ....
DropDownListFor
// ....
var dropDown = Mapper.GenerateHtmlWithoutMvcUnobtrusiveAttributes(metadata, htmlHelper.ViewContext.ViewData.TemplateInfo,
(propName) => htmlHelper.DropDownList(propName, selectList, optionLabel, attributes));
// ....
CheckBoxFor
// ....
var checkBox = Mapper.GenerateHtmlWithoutMvcUnobtrusiveAttributes(metadata, htmlHelper.ViewContext.ViewData.TemplateInfo,
(propName) => htmlHelper.CheckBox(propName, value, attributes));
// ....
With this approach in place all existing unit tests (alas still not a full suite) pass and initial hands-on tests look good.
If you wanted to fork and issue a pull request with the above changes in I'd happily take it. Alternately I'm happy to check my own changes in with this in place. Just wanted to give you the opportunity to have credit it for this.
from jquery.validation.unobtrusive.native.
Fixed with #2
from jquery.validation.unobtrusive.native.
Related Issues (20)
- Wrong version of .NET Framework HOT 1
- Add support for Regex HOT 10
- System.Web.Mvc.Html.SelectExtensions HOT 5
- Client side validation attributes generated for integer fields is incorrect.
- Remote validation ignores additionalFields HOT 2
- No Html.EditroFor/DisplayFor overloads HOT 1
- Migrate Demo project to use Bower for client side depencies HOT 1
- Validation fail if input name is Number.
- Adding HTML5 constraint attributes?
- Migrate Globalize to 1.x HOT 1
- Checkbox expected behaviour
- Support for EnumDropDownListFor HOT 4
- All checkboxes are being required regardless of [Required] attribute HOT 5
- JVun: Dynamic Advanced Demo Link Broken
- ASP.NEt Core HOT 1
- ValidationMessage and ValidationSummary HOT 1
- Bug: popover error message shows and disappears if you submit the form by hitting Enter
- Validation won't work with multi level models HOT 3
- Validation not working on IE8 HOT 4
- CheckBoxFor MVC5 - hidden? HOT 13
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jquery.validation.unobtrusive.native.