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

Partial validation with required-not-nullable values #130

Closed
carl-berg opened this issue Oct 1, 2015 · 3 comments
Closed

Partial validation with required-not-nullable values #130

carl-berg opened this issue Oct 1, 2015 · 3 comments

Comments

@carl-berg
Copy link

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?

@carl-berg
Copy link
Author

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()?

@JeremySkinner
Copy link
Member

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).

@carl-berg
Copy link
Author

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).

@lock lock bot locked and limited conversation to collaborators Aug 21, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants