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
Fluent Validation and MVC conflict #127
Comments
I think I've discovered what the problem is... The only two fields validating are integers, so I presume MVC is trying to set null in both fields hence the error message (I wrongfully thought MVC would bind the default values for the properties of my viewModel, like '0' for integers, 'false' for boolean and so on) [Display(Name = "Client", ResourceType = typeof(FieldNames))]
public int ClientId { get; set; }
[Display(Name = "DataPlan", ResourceType = typeof(FieldNames))]
public int DataPlan { get; set; } What I couldn't understand at first is why only the error messages for those two fields were shown and not for the others invalid fields. (When those fields were filled the rest of the messages would appear). The behaviour that I think could be improved is to show all validations only once when the model is not valid. But I think its something by design as FluentValidation leverages MVC validation pipeline - then it makes sense that MVC runs its own validations first. PS: I'm closing this issue, but I think would be nice to keep post for future reference, as this behaviour may not be clear for begginers. |
Yes, you're correct. This is one of the limitations of MVC's validation infrastructure (that'll hopefully be changed in the next version). Essentially, MVC expects validation to happen 1 property at a time, whilst FV expects all the properties to be set first and then validation to happen all at once (to properly support cross-property validation etc). This limitation can mostly be worked around, except in the case of non-nullable value types (int, DateTime etc). In this case, MVC will always add a validation message (and prevent any further validation from occurring) if the property doesn't have a value specified. To get around this, you must either:
|
I don't know, I'm probably missing something or there is a really weird behavior I'm not aware of...
I built three views with distinct viewModels and each one has its own validation class.
If I go to the two views I built first, leave all fields empty and submit the form, all validations are triggered correctlty. But the third one only triggers the validation for two fields
(ModelState.IsValid returns false and the ModelState only has errors for those two fields).
After a long time burning my brains off trying to understand the problem, I started changing the validations orders expecting a different result - no success. So I commented the rules and the validator attribute in the viewModel...
For my surprise, those two fields keep showing the validations even without no validation!!
I thought could be something related to cache - Changed browser, same result.
What am I missing here? The only two fields triggering validation: ClientId and DataPlan
The text was updated successfully, but these errors were encountered: