Comments (3)
Hi Marc,
Could you provide a more detailed reproduction of the issue please? I don't fully understand the problem at present. If you could put up a GitHub repo that demonstrated the issue that would be helpful.
Best,
John
from jquery.validation.unobtrusive.native.
Hi
I created a new MVC project to show the issue.
Here's the GitHub repo https://github.com/mmefriantine/jqueryvalidationissue7/tree/master/ValidationIssue
I edited Register.cshtml and AccountViewModel.cs to add
public class RegisterViewModelLevel2
{
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
}
public class RegisterViewModel
{
public RegisterViewModelLevel2 model { get; set; }
}
If you try to register, you see that even if the passwords match, you have an error. This behavior doesn't happen when you use the default validation by setting the control to false.
Please let me know if you have any questions.
Thank you
Marc
from jquery.validation.unobtrusive.native.
Hi Marc,
I've taken a look and I see the problem. It only affects the Compare / equalTo validation. As far as I'm aware all other types of validation work fine in the nested scenario.
The issue comes down to this line of code:
var equalToSelector = "#" + rule.ValidationParameters["other"].ToString().Substring(2);
This essentially takes the equalTo selector and converts it from the "*.PasswordDemo" style to "#PasswordDemo" style. The underlying assumption for this selector is that the id of the comparison element shares the same name as the "PasswordDemo" portion that comes from the ASP.Net validator code. This is true most of the time - ie when the model is not nested.
However, as you've identified this falls over when you come to nested models.
So what to do?
I've come up with a possibility and I'm not sure if it's a good idea or not at the moment. It would involve replacing the line of code mentioned above with this line instead:
equalToSelector = "[name$='" + rule.ValidationParameters["other"].ToString().Substring(2) + "']"
This would create an name-ends-with-selector which would look something like this:
data-rule-equalto="[name$='PasswordDemo']"
This would try and select the other element using the name. It would pick up any element which ended with (in this example) "PasswordDemo". So it works for your use case. (I've tried it out.) Where it would be a problem is if you had multiple elements on the screen with names that ended with "PasswordDemo". This is unlikely but it's possible.
An alternative is that I could also step in here and implement a JavaScript solution which was more robust. However, one thing I'm pretty committed to with jVUN is not to introduce my own version of jquery.validate.unobtrusive.js. I want people able to use jquery.validate.js in the raw without my own extra bells and whistles in tow.
So I'm not sure if it's a good idea and if it's something I want to implement. Do you have any brighter ideas for ways we could modify the data-rule-equalto
selector? I'm open to suggestions.
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 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.