Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using data-rule-require_from_group in CDTS 5.0.0 #1101

Open
legaren opened this issue Feb 15, 2024 · 4 comments
Open

Using data-rule-require_from_group in CDTS 5.0.0 #1101

legaren opened this issue Feb 15, 2024 · 4 comments

Comments

@legaren
Copy link

legaren commented Feb 15, 2024

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);
}
}

@sgdowney
Copy link

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.

@sgdowney
Copy link

You could try including this file to see if that resolves the issue without using the override.
additional-methods-require-from-group.zip

@legaren
Copy link
Author

legaren commented Feb 20, 2024

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

@sgdowney
Copy link

sgdowney commented Mar 1, 2024

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.

jquery-validation/jquery-validation#2482

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants