-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #94 from HtmlTags/validators
Validator support
- Loading branch information
Showing
19 changed files
with
186 additions
and
45 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
59 changes: 59 additions & 0 deletions
59
src/HtmlTags.AspNetCore/Conventions/Elements/Builders/ValidationMessageBuilder.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,59 @@ | ||
using System; | ||
using System.Linq; | ||
using Microsoft.AspNetCore.Mvc.ModelBinding; | ||
using Microsoft.AspNetCore.Mvc.Rendering; | ||
using Microsoft.AspNetCore.Mvc.ViewFeatures; | ||
using Microsoft.AspNetCore.Mvc.ViewFeatures.Internal; | ||
|
||
namespace HtmlTags.Conventions.Elements.Builders | ||
{ | ||
public class DefaultValidationMessageBuilder : IElementBuilder | ||
{ | ||
public HtmlTag Build(ElementRequest request) | ||
{ | ||
var viewContext = request.Get<ViewContext>() ?? throw new InvalidOperationException("Validation messages require a ViewContext"); | ||
|
||
var formContext = viewContext.ClientValidationEnabled ? viewContext.FormContext : null; | ||
if (!viewContext.ViewData.ModelState.ContainsKey(request.ElementId) && formContext == null) | ||
{ | ||
return HtmlTag.Empty(); | ||
} | ||
|
||
var tryGetModelStateResult = viewContext.ViewData.ModelState.TryGetValue(request.ElementId, out var entry); | ||
var modelErrors = tryGetModelStateResult ? entry.Errors : null; | ||
|
||
ModelError modelError = null; | ||
if (modelErrors != null && modelErrors.Count != 0) | ||
{ | ||
modelError = modelErrors.FirstOrDefault(m => !string.IsNullOrEmpty(m.ErrorMessage)) ?? modelErrors[0]; | ||
} | ||
|
||
if (modelError == null && formContext == null) | ||
{ | ||
return HtmlTag.Empty(); | ||
} | ||
|
||
var tag = new HtmlTag(viewContext.ValidationMessageElement); | ||
|
||
var className = modelError != null ? | ||
HtmlHelper.ValidationMessageCssClassName : | ||
HtmlHelper.ValidationMessageValidCssClassName; | ||
tag.AddClass(className); | ||
|
||
if (modelError != null) | ||
{ | ||
var modelExplorer = request.Get<ModelExplorer>() ?? throw new InvalidOperationException("Validation messages require a ModelExplorer"); | ||
tag.Text(ValidationHelpers.GetModelErrorMessageOrDefault(modelError, entry, modelExplorer)); | ||
} | ||
|
||
if (formContext != null) | ||
{ | ||
tag.Attr("data-valmsg-for", request.ElementId); | ||
|
||
tag.Attr("data-valmsg-replace", "true"); | ||
} | ||
|
||
return tag; | ||
} | ||
} | ||
} |
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,17 @@ | ||
using HtmlTags.Conventions; | ||
using HtmlTags.Conventions.Elements; | ||
using HtmlTags.Conventions.Elements.Builders; | ||
|
||
namespace HtmlTags | ||
{ | ||
public static class ModelStateTagExtensions | ||
{ | ||
public static HtmlConventionRegistry ModelState(this HtmlConventionRegistry registry) | ||
{ | ||
registry.ValidationMessages.Always.BuildBy<DefaultValidationMessageBuilder>(); | ||
registry.ValidationMessages.NamingConvention(new DotNotationElementNamingConvention()); | ||
|
||
return registry; | ||
} | ||
} | ||
} |
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,11 @@ | ||
namespace HtmlTags | ||
{ | ||
using Conventions.Elements; | ||
using Microsoft.AspNetCore.Razor.TagHelpers; | ||
|
||
[HtmlTargetElement("validation-message-tag", Attributes = ForAttributeName, TagStructure = TagStructure.WithoutEndTag)] | ||
public class ValidationMessageTagHelper : HtmlTagTagHelper | ||
{ | ||
protected override string Category { get; } = ElementConstants.ValidationMessage; | ||
} | ||
} |
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
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
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
Oops, something went wrong.