-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Improve handling of custom ValidationAttribute
s and their ValidationResult
s
#3595
Comments
I would have to recommend implementing IValidatableObject on your model class and using the new nameof operator to guard against property renames. Edit 2016-01-14: I needed this functionality myself today. Here is my example in case anyone else needs it for reference:
|
Thanks, That works, and its better than putting the validation in the controller. However, I prefer the DataAnnotations approach as it doesn't pollute to the view model so much. Is this something that should be fixed in MVC? |
The Maybe that method could be modified to check if the property name starts with the opening square bracket and concatenate appropriately? |
@tuespetre is correct. This looks like a straightforward bug. We should also scan the MVC repo for similar over-simplifications. FYI we have code in |
I found out why I was only getting one ModelState error. The It should return one for each member name specified. |
Though the relevant code has changed significantly, the symptoms remain in But the picture looks a bit wonky, especially when validating collection elements. This includes the original case of a ProblemsIn the context of MVC's calls to public ValidationResult GetValidationResult(object, ValidationContext)
RecommendationsWe should not fix (1.) because custom We should fix (2.) through (4.) because the current behaviour is confusing and users have no workarounds. DetailsNote 1The main MVC 5 -> ASP.NET Core MVC difference is that MVC 5 uselessly passes the same instance for Note 2The referenced string[] memberNames = validationContext.MemberName != null
? new string[] { validationContext.MemberName }
: null; Note 3The available information about Note 4An example of a public class MyModel
{
public ElementModel[] UniqueElements { get; set; }
}
[IsNotValid]
public class ElementModel
{
public string Name { get; set; }
}
public class IsNotValidAttribute : ValidationAttribute
{
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
return new ValidationResult(...);
}
} In the above |
…onResult`s - #3595 sub-items 2 through 4 - handle an indexer name in `ValidationResult.MemberNames` - aligns `ModelNames.CreatePropertyModelName()` with `TemplateInfo.GetFullHtmlFieldName()` - handle multiple elements in `ValidationResult.MemberNames` - later elements previously ignored - set `ValidationContext.MemberName` to `null` when no property name is available - using type name for a member name was just wrong
Following recommendations above, #4828 does not fix my "Problem 1". Most |
…onResult`s - #3595 sub-items 2 through 4 - handle an indexer name in `ValidationResult.MemberNames` - aligns `ModelNames.CreatePropertyModelName()` with `TemplateInfo.GetFullHtmlFieldName()` - handle multiple elements in `ValidationResult.MemberNames` - later elements previously ignored - set `ValidationContext.MemberName` to `null` when no property name is available - using type name for a member name was just wrong
Thanks @dougbu! |
ValidationAttribute
s and their ValidationResult
s
Hi
I'm trying to move some validation logic into a
ValidationAttribute
and I've come to a dead end. The idea is to mark aList<string>
to contain only unique entries.For example:
However, when returning the following validation result ...
... I would expect to see ModelState errors with the keys
"Values[1]"
and"Values[4]"
. Instead there is only one error with the key"Values.[1]"
. (Note the.
)I could put the attribute on the class and return a result like
...., new[] { "Values[1]" });
but that would require a bit of reflection and it breaks when you rename the property.Is there another (nice) way to achieve the same result??
I've traced it down to here:
Mvc/src/Microsoft.AspNet.Mvc.Core/ModelBinding/Validation/ValidationVisitor.cs
Line 119 in f7a211c
The text was updated successfully, but these errors were encountered: