You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There might be something i am missing but when i compare these two scenarios of posting the same empty model, i would expect them to return the same ModelState, but they don't:
public class TestModel
{
[Required]
public Guid Id { get; set; }
[Required]
public string Name { get; set; }
}
... here i get a required validation error for each property.
public class TestModel
{
public Guid Id { get; set; }
public string Name { get; set; }
}
public class TestModelValidator : AbstractValidator<TestModel>
{
public TestModelValidator()
{
RuleFor(x => x.Id).NotEmpty();
RuleFor(x => x.Name).NotEmpty();
}
}
... here i get a validation error only for the Id-property. What i would expect is to get validation errors for both properties (the same happens with .NotNull() for Name). Is there something i'm missing?
The text was updated successfully, but these errors were encountered:
After reading a bit more, I think this is the same as #127, which means that the problem is MVC-related. I'm a bit curious though to why using [Required] works as opposed to NotEmpty()?
Yes, this is a limitation of MVC's validation infrastructure.
The difference is because DataAnnotations works by validating each property in turn, but FluentValidation expects all the properties to be set first, and then validation is run only once over the entire object. MVC's validation infrastructure doesn't work properly with FluentValidation's approach - it generates the required message for non-nullable value types, but then cancels all object-level validation (so FV never actually runs).
Thanks for a quick reply! That's good to know. Then i can either change the offending properties to be nullable or perform my own validation later (after modelbinding).
There might be something i am missing but when i compare these two scenarios of posting the same empty model, i would expect them to return the same ModelState, but they don't:
... here i get a required validation error for each property.
... here i get a validation error only for the Id-property. What i would expect is to get validation errors for both properties (the same happens with
.NotNull()
for Name). Is there something i'm missing?The text was updated successfully, but these errors were encountered: