Skip to content

Commit c2a3fdf

Browse files
committed
Add missing registration for IValidatorFactory in AddFluentValidationClientsideAdapters.
1 parent 4846a78 commit c2a3fdf

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

src/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project>
22
<PropertyGroup>
3-
<VersionPrefix>11.1.1</VersionPrefix>
3+
<VersionPrefix>11.1.2</VersionPrefix>
44
<VersionSuffix></VersionSuffix>
55
<!-- Use CI build number as version suffix (if defined) -->
66
<!--<VersionSuffix Condition="'$(GITHUB_RUN_NUMBER)'!=''">ci-$(GITHUB_RUN_NUMBER)</VersionSuffix>-->

src/FluentValidation.AspNetCore/FluentValidationMvcExtensions.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ public static IServiceCollection AddFluentValidationClientsideAdapters(this ISer
148148
services.AddHttpContextAccessor();
149149
services.TryAddSingleton(ValidatorOptions.Global);
150150

151+
#pragma warning disable CS0618
152+
services.TryAddScoped<IValidatorFactory, ServiceProviderValidatorFactory>();
153+
#pragma warning restore CS0618
154+
151155
services.TryAddEnumerable(ServiceDescriptor.Singleton<IConfigureOptions<MvcViewOptions>, FluentValidationViewOptionsSetup>(s => {
152156
return new FluentValidationViewOptionsSetup(configuration, s.GetService<IHttpContextAccessor>());
153157
}));

src/FluentValidation.Tests.AspNetCore/ClientsideMessageTester.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
namespace FluentValidation.Tests;
2222

23+
using System;
2324
using System.Linq;
2425
using System.Net.Http;
2526
using System.Threading.Tasks;
@@ -30,9 +31,12 @@ namespace FluentValidation.Tests;
3031
using Xunit;
3132

3233
public class ClientsideMessageTester : IClassFixture<WebAppFixture> {
34+
private readonly WebAppFixture _webApp;
3335
private readonly HttpClient _client;
3436

3537
public ClientsideMessageTester(WebAppFixture webApp) {
38+
_webApp = webApp;
39+
3640
_client = webApp.CreateClientWithServices(services => {
3741
#pragma warning disable CS0618
3842
services.AddMvc().AddNewtonsoftJson().AddFluentValidation();
@@ -45,6 +49,23 @@ public ClientsideMessageTester(WebAppFixture webApp) {
4549
CultureScope.SetDefaultCulture();
4650
}
4751

52+
[Fact]
53+
public async Task Works_with_AddFluentValidationClientsideAdapters() {
54+
// This version of the test uses AddFluentValidationClientsideAdapters
55+
// rather than AddFluentValidation()
56+
var client = _webApp.CreateClientWithServices(services => {
57+
services.AddMvc();
58+
services.AddFluentValidationClientsideAdapters();
59+
services.AddValidatorsFromAssemblyContaining<TestController>();
60+
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
61+
services.AddScoped<ClientsideScopedDependency>();
62+
services.AddLocalization(opts => { opts.ResourcesPath = "Resources"; });
63+
});
64+
65+
var msg = await client.GetClientsideMessage("Required", "data-val-required");
66+
msg.ShouldEqual("'Required' must not be empty.");
67+
}
68+
4869
[Fact]
4970
public async Task NotEmpty_uses_simplified_message_for_clientside_validation() {
5071
var msg = await _client.GetClientsideMessage("Required", "data-val-required");

0 commit comments

Comments
 (0)