-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add mechanism of overriding name used for date input error messages
Closes #282
- Loading branch information
Showing
10 changed files
with
129 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System; | ||
using GovUk.Frontend.AspNetCore.ModelBinding; | ||
|
||
namespace GovUk.Frontend.AspNetCore; | ||
|
||
/// <summary> | ||
/// An attribute that can specify the error message prefix to use in model binding from date input components. | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Parameter, AllowMultiple = false)] | ||
public sealed class DateInputAttribute : Attribute | ||
{ | ||
/// <summary> | ||
/// Gets or sets the prefix used in error messages. | ||
/// </summary> | ||
/// <remarks> | ||
/// This prefix is used at the start of error messages produced by <see cref="DateInputModelBinder"/> | ||
/// e.g. <c>{ErrorMessagePrefix} must be a real date</c> | ||
/// </remarks> | ||
public string? ErrorMessagePrefix { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
src/GovUk.Frontend.AspNetCore/ModelBinding/DateInputModelMetadata.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace GovUk.Frontend.AspNetCore.ModelBinding; | ||
|
||
internal sealed class DateInputModelMetadata | ||
{ | ||
public string? ErrorMessagePrefix { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/GovUk.Frontend.AspNetCore/ModelBinding/GovUkFrontendAspNetCoreMetadataDetailsProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System.Linq; | ||
using Microsoft.AspNetCore.Mvc.ModelBinding.Metadata; | ||
|
||
namespace GovUk.Frontend.AspNetCore.ModelBinding; | ||
|
||
internal class GovUkFrontendAspNetCoreMetadataDetailsProvider : IMetadataDetailsProvider, IDisplayMetadataProvider | ||
{ | ||
public void CreateDisplayMetadata(DisplayMetadataProviderContext context) | ||
{ | ||
var dateOnlyMetadataAttribute = context.Attributes.OfType<DateInputAttribute>().FirstOrDefault(); | ||
|
||
if (dateOnlyMetadataAttribute is not null) | ||
{ | ||
var dateOnlyMetadata = new DateInputModelMetadata() | ||
{ | ||
ErrorMessagePrefix = dateOnlyMetadataAttribute.ErrorMessagePrefix | ||
}; | ||
|
||
context.DisplayMetadata.AdditionalValues.Add(typeof(DateInputModelMetadata), dateOnlyMetadata); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters