1- @using Microsoft .AspNetCore .Html ;
1+ @using Microsoft .AspNetCore .Html ;
22@using DfE .ExternalApplications .Web .Utilities ;
33@model DfE .ExternalApplications .Web .Services .FieldViewModel
44@{
5- var maxLengthRule = Model .Field .Validations ? .Where (v => v .Type == " maxLength" ).FirstOrDefault ()? .Rule .ToString ();
5+ var maxLengthValidation = Model .Field .Validations ? .FirstOrDefault (v => v .Type == " maxLength" );
6+ var maxWordsValidation = Model .Field .Validations ? .FirstOrDefault (v => v .Type == " maxWords" );
7+
8+ var maxLengthRule = maxLengthValidation ? .Rule .ToString ();
9+ var maxWordsRule = maxWordsValidation ? .Rule .ToString ();
10+
611 int .TryParse (maxLengthRule , out var maxLengthValue );
12+ int .TryParse (maxWordsRule , out var maxWordsValue );
13+
14+ var useWordLimit = maxWordsValue > 0 ;
15+ var limitAttributeValue = useWordLimit ? maxWordsValue : maxLengthValue ;
16+
717 var labelClasses = Model .Field .Label .IsVisible ? " govuk-label govuk-label--m" : " govuk-visually-hidden" ;
8- var maxLengthMessage = Model . Field . Validations ? . Where ( v => v . Type == " maxLength " ). FirstOrDefault () ? .Message
18+ var maxLengthMessage = maxWordsValidation ? . Message ?? maxLengthValidation ? .Message
919 ?? " You have entered too many characters" ;
1020}
11- <govuk-character-count id =" @Model.Id" name =" Data[@Model.Field.FieldId]" max-length =" @maxLengthValue" >
12- <govuk-character-count-label class =" @labelClasses" asp-for =" @Model.Field.FieldId" >@Model.Field.Label.Value </govuk-character-count-label >
13- @{
14- var (hintHtml , hintClass ) = MarkdownSafe .RenderHintWithClass (Model .Field .Tooltip );
15- }
16- <govuk-character-count-hint class =" @hintClass" >
17- @Html.Raw(hintHtml)
18- </govuk-character-count-hint >
21+ @if (useWordLimit )
22+ {
23+ <govuk-character-count id =" @Model.Id" name =" Data[@Model.Field.FieldId]" max-words =" @limitAttributeValue" >
24+ <govuk-character-count-label class =" @labelClasses" asp-for =" @Model.Field.FieldId" >@Model.Field.Label.Value </govuk-character-count-label >
25+ @{
26+ var (hintHtml , hintClass ) = MarkdownSafe .RenderHintWithClass (Model .Field .Tooltip );
27+ }
28+ <govuk-character-count-hint class =" @hintClass" >
29+ @Html.Raw(hintHtml)
30+ </govuk-character-count-hint >
31+
32+ @if (Model .ErrorMessage != String .Empty )
33+ {
34+ <govuk-character-count-error-message >
35+ <span class =" govuk-visually-hidden" >Error : </span >
36+ @Model.ErrorMessage
37+ </govuk-character-count-error-message >
38+ }
39+ <govuk-character-count-value >@Model.CurrentValue </govuk-character-count-value >
40+ </govuk-character-count >
41+ }
42+ else
43+ {
44+ <govuk-character-count id =" @Model.Id" name =" Data[@Model.Field.FieldId]" max-length =" @limitAttributeValue" >
45+ <govuk-character-count-label class =" @labelClasses" asp-for =" @Model.Field.FieldId" >@Model.Field.Label.Value </govuk-character-count-label >
46+ @{
47+ var (hintHtml , hintClass ) = MarkdownSafe .RenderHintWithClass (Model .Field .Tooltip );
48+ }
49+ <govuk-character-count-hint class =" @hintClass" >
50+ @Html.Raw(hintHtml)
51+ </govuk-character-count-hint >
1952
20- @if (Model .ErrorMessage != String .Empty )
21- {
22- <govuk-character-count-error-message >
23- <span class =" govuk-visually-hidden" >Error : </span >
24- @Model.ErrorMessage
25- </govuk-character-count-error-message >
26- }
27- <govuk-character-count-value >@Model.CurrentValue </govuk-character-count-value >
28- </govuk-character-count >
53+ @if (Model .ErrorMessage != String .Empty )
54+ {
55+ <govuk-character-count-error-message >
56+ <span class =" govuk-visually-hidden" >Error : </span >
57+ @Model.ErrorMessage
58+ </govuk-character-count-error-message >
59+ }
60+ <govuk-character-count-value >@Model.CurrentValue </govuk-character-count-value >
61+ </govuk-character-count >
62+ }
0 commit comments