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

Move the IValidationRule<T, TProperty>.SetDisplayName(string) method to the IValidationRule interface. #2179

Open
GillesTourreau opened this issue Dec 1, 2023 · 1 comment
Milestone

Comments

@GillesTourreau
Copy link

GillesTourreau commented Dec 1, 2023

Is your feature request related to a problem? Please describe.

When creating an inherited class from the AbstractValidator<T> it is not possible when overriding the OnRuleAdded() method, to change the display name of the property.

protected override void OnRuleAdded(IValidationRule<T> rule)
{
    // Unable to change the display name using rule.SetDisplayName()
}

It is also not possible to cast the rule into the IValidationRule<T, out TProperty> because the OnRuleAdded() is called for each property to validate.

Describe the solution you'd like

It will be great to move IValidationRule<T, TProperty>.SetDisplayName(string) method to the IValidationRule interface.

protected override void OnRuleAdded(IValidationRule<T> rule)
{
    rule.SetDisplayName("My display name property");
}

Describe alternatives you've considered

We can use reflection currently for this work around:

protected override void OnRuleAdded(IValidationRule<T> rule)
{
    var setDisplayNameMethod = rule.GetType().GetMethod("SetDisplayName", new[] { typeof(string) })!;
    setDisplayNameMethod.Invoke(rule, new object[] { "My display name property" });
}

Additional Context

No response

@JeremySkinner
Copy link
Member

Thanks for the suggestion. Seems like a reasonable idea, although it would need to wait until 12.0 as this is a breaking change.

@JeremySkinner JeremySkinner added this to the 12.0 milestone Dec 2, 2023
@JeremySkinner JeremySkinner changed the title Move the IValidationRule<T, TProperty>.GetDisplayName(string) method to the IValidationRule interface. Move the IValidationRule<T, TProperty>.SetDisplayName(string) method to the IValidationRule interface. Feb 6, 2024
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