diff --git a/src/GovUk.Frontend.AspNetCore/GovUkFrontendAspNetCoreOptions.cs b/src/GovUk.Frontend.AspNetCore/GovUkFrontendAspNetCoreOptions.cs index 945806d5..fc6eb102 100644 --- a/src/GovUk.Frontend.AspNetCore/GovUkFrontendAspNetCoreOptions.cs +++ b/src/GovUk.Frontend.AspNetCore/GovUkFrontendAspNetCoreOptions.cs @@ -1,8 +1,10 @@ using System; using System.Collections.Generic; using GovUk.Frontend.AspNetCore.HtmlGeneration; +using GovUk.Frontend.AspNetCore.ModelBinding; using GovUk.Frontend.AspNetCore.TagHelpers; using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc.ModelBinding; namespace GovUk.Frontend.AspNetCore { @@ -75,5 +77,35 @@ public GovUkFrontendAspNetCoreOptions() /// The default is true. /// public bool PrependErrorToTitle { get; set; } + + /// + /// Creates an for Date input components using the + /// registered in for the specified . + /// + /// The type that the created should support. + /// + /// A + /// or if there is no registered that supports . + /// + public IModelBinder? GetDateInputModelBinder(Type dateModelType) + { + foreach (var converter in DateInputModelConverters) + { + if (converter.CanConvertModelType(dateModelType)) + { + return GetDateInputModelBinder(converter); + } + } + + return null; + } + + /// + /// Creates an for Date input components using the specified . + /// + /// The to use to convert to and from a . + /// A . + public IModelBinder GetDateInputModelBinder(DateInputModelConverter converter) => + new DateInputModelBinder(converter); } } diff --git a/src/GovUk.Frontend.AspNetCore/ModelBinding/DateInputModelBinderProvider.cs b/src/GovUk.Frontend.AspNetCore/ModelBinding/DateInputModelBinderProvider.cs index 5329c389..0218afea 100644 --- a/src/GovUk.Frontend.AspNetCore/ModelBinding/DateInputModelBinderProvider.cs +++ b/src/GovUk.Frontend.AspNetCore/ModelBinding/DateInputModelBinderProvider.cs @@ -4,13 +4,13 @@ namespace GovUk.Frontend.AspNetCore.ModelBinding { internal class DateInputModelBinderProvider : IModelBinderProvider { - private readonly DateInputModelConverter[] _dateInputModelConverters; + private readonly GovUkFrontendAspNetCoreOptions _options; public DateInputModelBinderProvider(GovUkFrontendAspNetCoreOptions options) { Guard.ArgumentNotNull(nameof(options), options); - _dateInputModelConverters = options.DateInputModelConverters.ToArray(); + _options = options; } public IModelBinder? GetBinder(ModelBinderProviderContext context) @@ -18,16 +18,7 @@ public DateInputModelBinderProvider(GovUkFrontendAspNetCoreOptions options) Guard.ArgumentNotNull(nameof(context), context); var modelType = context.Metadata.UnderlyingOrModelType; - - foreach (var converter in _dateInputModelConverters) - { - if (converter.CanConvertModelType(modelType)) - { - return new DateInputModelBinder(converter); - } - } - - return null; + return _options.GetDateInputModelBinder(modelType); } } }