-
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
9 changed files
with
122 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
20 changes: 20 additions & 0 deletions
20
src/GovUk.Frontend.AspNetCore/DateInputMetadataAttribute.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,20 @@ | ||
using System; | ||
using GovUk.Frontend.AspNetCore.ModelBinding; | ||
|
||
namespace GovUk.Frontend.AspNetCore; | ||
|
||
/// <summary> | ||
/// Provides additional information for model binding with date input components. | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property, AllowMultiple = false)] | ||
public sealed class DateInputMetadataAttribute : Attribute | ||
{ | ||
/// <summary> | ||
/// Gets or sets the field description used in error messages. | ||
/// </summary> | ||
/// <remarks> | ||
/// This description is used at the start of error messages produced by <see cref="DateInputModelBinder"/> | ||
/// e.g. <c>{ErrorMessagesDescription} must be a real date</c> | ||
/// </remarks> | ||
public string? ErrorMessagesDescription { 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? ErrorMessagesDescription { 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<DateInputMetadataAttribute>().FirstOrDefault(); | ||
|
||
if (dateOnlyMetadataAttribute is not null) | ||
{ | ||
var dateOnlyMetadata = new DateInputModelMetadata() | ||
{ | ||
ErrorMessagesDescription = dateOnlyMetadataAttribute.ErrorMessagesDescription | ||
}; | ||
|
||
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