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

ValidationContext.MemberName is set incorrectly in the DataAnnotationsModelValidator #120

Closed
mkArtakMSFT opened this issue Jan 23, 2018 · 6 comments
Assignees

Comments

@mkArtakMSFT
Copy link
Member

mkArtakMSFT commented Jan 23, 2018

Problem Description

My customer creating custom validation class which deliveerd from System.ComponentModel.DataAnnotations.ValidationAttribute.
Using this in .NET4.7.1 ASP.NET Web API 2.0, ValidationContext.MemberName is set "DisplayName".
This is problem for custom class because they can not know MemberName without this property.

  • Expected behavior
    ValidationContext.MemberName is "TestValue3"
  • Actual behavior
    ValidationContext.MemberName is "Display name From 'DisplayAttribute'"

TestValue3 is set "DisplayAttribute".

    public class TestModel
    {
        [Validations.CustomValidation]
        public string TestValue1 { get; set; }
        [Validations.CustomValidation(ErrorMessage = "ErrorMessage: {0} ErrorMessage")]
        public string TestValue2 { get; set; }
        [Validations.CustomValidation(ErrorMessage = "ErrorMessage: {0} ErrorMessage")]
        [Display(Name = "Display name From 'DisplayAttribute'")]
        public string TestValue3 { get; set; }
        public string Message { get; set; }
    }
@mkArtakMSFT mkArtakMSFT added this to the 3.2.4-preview2 milestone Jan 23, 2018
@mkArtakMSFT
Copy link
Member Author

@dougbu, please associate the fix we've discussed with this bug. //cc: @danroth27

@dougbu
Copy link
Member

dougbu commented Jan 23, 2018

For historical interest: Was a PR for this in the old CodePlex repo http://aspnetwebstack.codeplex.com/SourceControl/network/forks/astolfoho/fixWebApi/contribution/8918. But, I can't find an earlier issue on this.

dougbu added a commit that referenced this issue Jan 23, 2018
- #120

`ValidationAttribute` classes that (incorrectly) expected `MemberName` to always be set or that
instantiate `ValidationResult`s using the `DisplayName` may be negatively impacted. For exmaple,
`MemberName` will be `null` when binding parameters and items in a collection.
@dougbu dougbu modified the milestones: 3.2.4-preview2, 3.2.5 Jan 29, 2018
@dougbu
Copy link
Member

dougbu commented Jan 29, 2018

Moved to 3.2.5 (really 3.2.5-preview1).

dougbu added a commit that referenced this issue Feb 2, 2018
- #120
- revert to previous behavior if `webapi:UseLegacyValidationMemberName` application setting is `true`
@dougbu
Copy link
Member

dougbu commented Feb 2, 2018

9829dd8

@dougbu dougbu closed this as completed Feb 2, 2018
@kraeg75
Copy link

kraeg75 commented Jun 7, 2018

I still see this problem.
My Web API references System.Web.Http in the Microsoft.AspNet.WebApi.Core.5.2.4 NuGet package which was published on May 21, 2018. The System.Web.Http dll itself is dated Feb 1.
The 9829dd8 commit above is dated Jan 24, which leads me to believe the dll should include the fix.

@dougbu
Copy link
Member

dougbu commented Jun 7, 2018

This fix is in MVC 5.2.5 preview 1 and later, not 5.2.4.

FYI the commit date may be much earlier than when the changes are merged into the master branch.

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

No branches or pull requests

3 participants