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

TryValidateModel does not work as expected #53

Open
icnocop opened this issue Jan 19, 2025 · 2 comments · May be fixed by #54
Open

TryValidateModel does not work as expected #53

icnocop opened this issue Jan 19, 2025 · 2 comments · May be fixed by #54

Comments

@icnocop
Copy link
Contributor

icnocop commented Jan 19, 2025

Hi.

When integrating with ASP.NET Core MVC, a call to TryValidateModel does not work as expected.

    public class Test1Controller : Controller
    {
        public ActionResult Action1(Action1ViewModel viewModel)
        {
            // re-validate model
            this.ModelState.Clear();
            this.TryValidateModel(viewModel);

            if (!this.ModelState.IsValid)
            {
                return this.View(viewModel);
            }

            ...
        }
    }

In my actual scenario, ModelState.Entries is 18 and ModelState.IsValid is false when the action method is called, as expected.

ModelState.Clear() will set Entries to 0 and IsValid to true, as expected.

When using FluentValidation.AspNetCore, a call to TryValidateModel(viewModel); will re-evaluate the model and set ModelState.Entries to 18 and ModelState.IsValid to false, for example.

I expected SharpGrip.FluentValidation.AutoValidation to do the same, but Entries is 0 and IsValid is true, as if the view model was not re-evaluated/validated, which is unexpected.

Thank you.

icnocop added a commit to icnocop/FluentValidation.AutoValidation that referenced this issue Jan 19, 2025
icnocop added a commit to icnocop/FluentValidation.AutoValidation that referenced this issue Jan 19, 2025
@icnocop icnocop linked a pull request Jan 19, 2025 that will close this issue
icnocop added a commit to icnocop/FluentValidation.AutoValidation that referenced this issue Jan 19, 2025
@mvdgun
Copy link
Member

mvdgun commented Jan 19, 2025

Hi @icnocop, thanks again :). Is this one also related to #52?

@icnocop
Copy link
Contributor Author

icnocop commented Jan 19, 2025

Hi.

I would classify this as a different issue, not directly related to #52, but it may require #52 so the code gets far enough to be able to call TryValidateModel in a real scenario.

Thank you.

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

Successfully merging a pull request may close this issue.

2 participants