Open legaren opened 9 months ago
It's unfortunate that jQuery Form Validation uses underscores in the attribute names. Technically underscores shouldn't be allowed, which is why the attribute name is changed by the HTML Helper. The proper fix would be to update the attribute in the additional-methods.js file to replace the underscores with dashes then the override above would not be required.
You could try including this file to see if that resolves the issue without using the override. additional-methods-require-from-group.zip
Thanks for the input @sgdowney .
My project really isn't setup well enough to just be able to import that change in, I also want to only get files from canada.ca. I did try another trick as proof on concept. Given that your updated file is exactly the same as the original method with the rule name changed, I thought maybe I could update the internal collection with the new name, which is what I did with this code:
if ($.validator.methods.hasOwnProperty("require_from_group")) {
$.validator.methods["require-from-group"] = $.validator.methods["require_from_group"];
delete $.validator.methods["require_from_group"];
}
if ($.validator.messages.hasOwnProperty("require_from_group")) {
$.validator.messages["require-from-group"] = $.validator.messages["require_from_group"];
delete $.validator.messages["require_from_group"];
}
This worked as expected. I realize this is incomplete as there are impacts to other internal objects which I'm not handling. Is there any way the original code could be altered in this repository so that it handles both names? My goal is to have a solution provided by canada.ca, but I don't know about the release cycle, etc...
Thank you
I've opened an issue in the jquery validate repository to enquire on the feasibility of updating that rule to use dashes instead of underscores. If that change can be made, then WET would update the version of jquery validate to resolve this issue.
https://github.com/jquery-validation/jquery-validation/issues/2482
I am trying to use the validation attribute "data-rule-require_from_group", in order to generate my field I use the html helper as follows:
@Html.EditorFor(model => model.FirstName, "First Name", new { htmlAttributes = new { @class = "group", data_rule_require_from_group = "[1,\".group\"]" } })
Any attribute passed in to the htmlAttributes parameter will see its underscores replaced by dashes as dashes are not permitted in the name portion of the attribute (in C#). Unfortunately the "data-rule-require_from_group" contains both. I believe there is no syntax to pass in an actual underscore. I have resorted to overriding the DefaultHtmlGenerator so that I can handle this specific attribute properly. It actually works but it would be nice to have CDTS do it for me.
I am leaving partial code here as reference, perhaps someone could implement a proper fix.
///
/// Fixes issues with the "data-rule-require_from_group" attribute
///
public override TagBuilder GenerateSelect(ViewContext viewContext, ModelExplorer modelExplorer, string optionLabel, string expression, IEnumerable selectList, ICollection currentValues, bool allowMultiple, object htmlAttributes)
{
TagBuilder tag = base.GenerateSelect(viewContext, modelExplorer, optionLabel, expression, selectList, currentValues, allowMultiple, htmlAttributes);
FixRequireFromGroup(tag);
return tag;
}
///
/// Fixes issues with the "data-rule-require_from_group" attribute
///
protected override TagBuilder GenerateInput(ViewContext viewContext, InputType inputType, ModelExplorer modelExplorer, string expression, object value, bool useViewData, bool isChecked, bool setId, bool isExplicitValue, string format, IDictionary<string, object> htmlAttributes)
{
TagBuilder tag = base.GenerateInput(viewContext, inputType, modelExplorer, expression, value, useViewData, isChecked, setId, isExplicitValue, format, htmlAttributes);
FixRequireFromGroup(tag);
return tag;
}
///
/// Replaces the attribute with proper underscores (_)
///
private void FixRequireFromGroup(TagBuilder tag)
{
string? require_from_group = tag.Attributes.Keys.FirstOrDefault(i => i.Equals("data-rule-require-from-group", StringComparison.OrdinalIgnoreCase));
if (require_from_group != null)
{
string? val = tag.Attributes[require_from_group];
tag.Attributes.Remove(require_from_group);
tag.Attributes.Add("data-rule-require_from_group", val);
}
}