-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add field validation handlers to ensure field validation when content…
- Loading branch information
1 parent
e38076a
commit 7c2f230
Showing
16 changed files
with
526 additions
and
14 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
src/OrchardCore.Modules/OrchardCore.ContentFields/Handlers/ContentPickerFieldHandler.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,36 @@ | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Localization; | ||
using OrchardCore.ContentFields.Fields; | ||
using OrchardCore.ContentFields.Settings; | ||
using OrchardCore.ContentManagement.Handlers; | ||
using OrchardCore.ContentManagement.Metadata.Models; | ||
|
||
namespace OrchardCore.ContentFields.Handlers; | ||
|
||
public class ContentPickerFieldHandler : ContentFieldHandler<ContentPickerField> | ||
{ | ||
private readonly IStringLocalizer S; | ||
|
||
public ContentPickerFieldHandler(IStringLocalizer<ContentPickerFieldHandler> stringLocalizer) | ||
{ | ||
S = stringLocalizer; | ||
} | ||
|
||
public override Task ValidatingAsync(ValidateContentFieldContext context, ContentPickerField field) | ||
{ | ||
var settings = context.ContentPartFieldDefinition.GetSettings<ContentPickerFieldSettings>(); | ||
|
||
if (settings.Required && field.ContentItemIds.Length == 0) | ||
{ | ||
context.Fail(S["The {0} field is required.", context.ContentPartFieldDefinition.DisplayName()], nameof(field.ContentItemIds)); | ||
} | ||
|
||
if (!settings.Multiple && field.ContentItemIds.Length > 1) | ||
{ | ||
context.Fail(S["The {0} field cannot contain multiple items.", context.ContentPartFieldDefinition.DisplayName()], nameof(field.ContentItemIds)); | ||
} | ||
|
||
return Task.CompletedTask; | ||
} | ||
} | ||
|
30 changes: 30 additions & 0 deletions
30
src/OrchardCore.Modules/OrchardCore.ContentFields/Handlers/DateFieldHandler.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,30 @@ | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Localization; | ||
using OrchardCore.ContentFields.Fields; | ||
using OrchardCore.ContentFields.Settings; | ||
using OrchardCore.ContentManagement.Handlers; | ||
using OrchardCore.ContentManagement.Metadata.Models; | ||
|
||
namespace OrchardCore.ContentFields.Handlers; | ||
|
||
public class DateFieldHandler : ContentFieldHandler<DateField> | ||
{ | ||
private readonly IStringLocalizer S; | ||
|
||
public DateFieldHandler(IStringLocalizer<DateFieldHandler> stringLocalizer) | ||
{ | ||
S = stringLocalizer; | ||
} | ||
|
||
public override Task ValidatingAsync(ValidateContentFieldContext context, DateField field) | ||
{ | ||
var settings = context.ContentPartFieldDefinition.GetSettings<DateFieldSettings>(); | ||
|
||
if (settings.Required && !field.Value.HasValue) | ||
{ | ||
context.Fail(S["A value is required for {0}.", context.ContentPartFieldDefinition.DisplayName()], nameof(field.Value)); | ||
} | ||
|
||
return Task.CompletedTask; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/OrchardCore.Modules/OrchardCore.ContentFields/Handlers/DateTimeFieldHandler.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,30 @@ | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Localization; | ||
using OrchardCore.ContentFields.Fields; | ||
using OrchardCore.ContentFields.Settings; | ||
using OrchardCore.ContentManagement.Handlers; | ||
using OrchardCore.ContentManagement.Metadata.Models; | ||
|
||
namespace OrchardCore.ContentFields.Handlers; | ||
|
||
public class DateTimeFieldHandler : ContentFieldHandler<DateTimeField> | ||
{ | ||
private readonly IStringLocalizer S; | ||
|
||
public DateTimeFieldHandler(IStringLocalizer<DateTimeFieldHandler> stringLocalizer) | ||
{ | ||
S = stringLocalizer; | ||
} | ||
|
||
public override Task ValidatingAsync(ValidateContentFieldContext context, DateTimeField field) | ||
{ | ||
var settings = context.ContentPartFieldDefinition.GetSettings<DateTimeFieldSettings>(); | ||
|
||
if (settings.Required && !field.Value.HasValue) | ||
{ | ||
context.Fail(S["A value is required for {0}.", context.ContentPartFieldDefinition.DisplayName()], nameof(field.Value)); | ||
} | ||
|
||
return Task.CompletedTask; | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
src/OrchardCore.Modules/OrchardCore.ContentFields/Handlers/LinkFieldHandler.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,92 @@ | ||
using System; | ||
using System.Text.Encodings.Web; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Mvc.Infrastructure; | ||
using Microsoft.AspNetCore.Mvc.Routing; | ||
using Microsoft.Extensions.Localization; | ||
using OrchardCore.ContentFields.Fields; | ||
using OrchardCore.ContentFields.Settings; | ||
using OrchardCore.ContentManagement.Handlers; | ||
using OrchardCore.ContentManagement.Metadata.Models; | ||
using OrchardCore.Infrastructure.Html; | ||
|
||
namespace OrchardCore.ContentFields.Handlers; | ||
|
||
public class LinkFieldHandler : ContentFieldHandler<LinkField> | ||
{ | ||
private readonly IUrlHelperFactory _urlHelperFactory; | ||
private readonly IActionContextAccessor _actionContextAccessor; | ||
private readonly IStringLocalizer S; | ||
private readonly IHtmlSanitizerService _htmlSanitizerService; | ||
private readonly HtmlEncoder _htmlencoder; | ||
|
||
public LinkFieldHandler( | ||
IUrlHelperFactory urlHelperFactory, | ||
IActionContextAccessor actionContextAccessor, | ||
IStringLocalizer<LinkFieldHandler> localizer, | ||
IHtmlSanitizerService htmlSanitizerService, | ||
HtmlEncoder htmlencoder) | ||
{ | ||
_urlHelperFactory = urlHelperFactory; | ||
_actionContextAccessor = actionContextAccessor; | ||
S = localizer; | ||
_htmlSanitizerService = htmlSanitizerService; | ||
_htmlencoder = htmlencoder; | ||
} | ||
|
||
public override Task ValidatingAsync(ValidateContentFieldContext context, LinkField field) | ||
{ | ||
var settings = context.ContentPartFieldDefinition.GetSettings<LinkFieldSettings>(); | ||
|
||
var urlToValidate = field.Url; | ||
if (!String.IsNullOrEmpty(urlToValidate)) | ||
{ | ||
var indexAnchor = urlToValidate.IndexOf('#'); | ||
if (indexAnchor > -1) | ||
{ | ||
urlToValidate = urlToValidate.Substring(0, indexAnchor); | ||
} | ||
|
||
if (urlToValidate.StartsWith("~/", StringComparison.Ordinal)) | ||
{ | ||
var urlHelper = _urlHelperFactory.GetUrlHelper(_actionContextAccessor.ActionContext); | ||
urlToValidate = urlHelper.Content(urlToValidate); | ||
} | ||
|
||
urlToValidate = urlToValidate.ToUriComponents(); | ||
} | ||
|
||
// Validate Url | ||
if (settings.Required && String.IsNullOrWhiteSpace(field.Url)) | ||
{ | ||
context.Fail(S["The url is required for {0}.", context.ContentPartFieldDefinition.DisplayName()], nameof(field.Url)); | ||
} | ||
else if (!String.IsNullOrWhiteSpace(field.Url)) | ||
{ | ||
if (!Uri.IsWellFormedUriString(urlToValidate, UriKind.RelativeOrAbsolute)) | ||
{ | ||
context.Fail(S["{0} is an invalid url.", field.Url], nameof(field.Url)); | ||
} | ||
else | ||
{ | ||
var link = $"<a href=\"{_htmlencoder.Encode(urlToValidate)}\"></a>"; | ||
|
||
if (!String.Equals(link, _htmlSanitizerService.Sanitize(link), StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
context.Fail(S["{0} is an invalid url.", field.Url], nameof(field.Url)); | ||
} | ||
} | ||
} | ||
|
||
if (settings.LinkTextMode == LinkTextMode.Required && String.IsNullOrWhiteSpace(field.Text)) | ||
{ | ||
context.Fail(S["The link text is required for {0}.", context.ContentPartFieldDefinition.DisplayName()], nameof(field.Text)); | ||
} | ||
else if (settings.LinkTextMode == LinkTextMode.Static && String.IsNullOrWhiteSpace(settings.DefaultText)) | ||
{ | ||
context.Fail(S["The text default value is required for {0}.", context.ContentPartFieldDefinition.DisplayName()], nameof(field.Text)); | ||
} | ||
|
||
return Task.CompletedTask; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...re.Modules/OrchardCore.ContentFields/Handlers/LocalizationSetContentPickerFieldHandler.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,35 @@ | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Localization; | ||
using OrchardCore.ContentFields.Fields; | ||
using OrchardCore.ContentFields.Settings; | ||
using OrchardCore.ContentManagement.Handlers; | ||
using OrchardCore.ContentManagement.Metadata.Models; | ||
|
||
namespace OrchardCore.ContentFields.Handlers; | ||
|
||
public class LocalizationSetContentPickerFieldHandler : ContentFieldHandler<LocalizationSetContentPickerField> | ||
{ | ||
private readonly IStringLocalizer S; | ||
|
||
public LocalizationSetContentPickerFieldHandler(IStringLocalizer<LocalizationSetContentPickerFieldHandler> stringLocalizer) | ||
{ | ||
S = stringLocalizer; | ||
} | ||
|
||
public override Task ValidatingAsync(ValidateContentFieldContext context, LocalizationSetContentPickerField field) | ||
{ | ||
var settings = context.ContentPartFieldDefinition.GetSettings<LocalizationSetContentPickerFieldSettings>(); | ||
|
||
if (settings.Required && field.LocalizationSets.Length == 0) | ||
{ | ||
context.Fail(S["The {0} field is required.", context.ContentPartFieldDefinition.DisplayName()], nameof(field.LocalizationSets)); | ||
} | ||
|
||
if (!settings.Multiple && field.LocalizationSets.Length > 1) | ||
{ | ||
context.Fail(S["The {0} field cannot contain multiple items.", context.ContentPartFieldDefinition.DisplayName()], nameof(field.LocalizationSets)); | ||
} | ||
|
||
return Task.CompletedTask; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/OrchardCore.Modules/OrchardCore.ContentFields/Handlers/MultiTextFieldHandler.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,30 @@ | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Localization; | ||
using OrchardCore.ContentFields.Fields; | ||
using OrchardCore.ContentFields.Settings; | ||
using OrchardCore.ContentManagement.Handlers; | ||
using OrchardCore.ContentManagement.Metadata.Models; | ||
|
||
namespace OrchardCore.ContentFields.Handlers; | ||
|
||
public class MultiTextFieldHandler : ContentFieldHandler<MultiTextField> | ||
{ | ||
private readonly IStringLocalizer S; | ||
|
||
public MultiTextFieldHandler(IStringLocalizer<MultiTextFieldHandler> stringLocalizer) | ||
{ | ||
S = stringLocalizer; | ||
} | ||
|
||
public override Task ValidatingAsync(ValidateContentFieldContext context, MultiTextField field) | ||
{ | ||
var settings = context.ContentPartFieldDefinition.GetSettings<MultiTextFieldSettings>(); | ||
|
||
if (settings.Required && field.Values.Length == 0) | ||
{ | ||
context.Fail(S["A value is required for {0}.", context.ContentPartFieldDefinition.DisplayName()], nameof(field.Values)); | ||
} | ||
|
||
return Task.CompletedTask; | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
src/OrchardCore.Modules/OrchardCore.ContentFields/Handlers/NumericFieldHandler.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,57 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Localization; | ||
using OrchardCore.ContentFields.Fields; | ||
using OrchardCore.ContentFields.Settings; | ||
using OrchardCore.ContentManagement.Handlers; | ||
using OrchardCore.ContentManagement.Metadata.Models; | ||
|
||
namespace OrchardCore.ContentFields.Handlers; | ||
|
||
public class NumericFieldHandler : ContentFieldHandler<NumericField> | ||
{ | ||
private readonly IStringLocalizer S; | ||
|
||
public NumericFieldHandler(IStringLocalizer<NumericFieldHandler> stringLocalizer) | ||
{ | ||
S = stringLocalizer; | ||
} | ||
|
||
public override Task ValidatingAsync(ValidateContentFieldContext context, NumericField field) | ||
{ | ||
var settings = context.ContentPartFieldDefinition.GetSettings<NumericFieldSettings>(); | ||
|
||
if (settings.Required && !field.Value.HasValue) | ||
{ | ||
context.Fail(S["A value is required for {0}.", context.ContentPartFieldDefinition.DisplayName()], nameof(field.Value)); | ||
} | ||
|
||
if (field.Value.HasValue) | ||
{ | ||
if (settings.Minimum.HasValue && field.Value.Value < settings.Minimum.Value) | ||
{ | ||
context.Fail(S["The value must be greater than {0}.", settings.Minimum.Value], nameof(field.Value)); | ||
} | ||
|
||
if (settings.Maximum.HasValue && field.Value.Value > settings.Maximum.Value) | ||
{ | ||
context.Fail(S["The value must be less than {0}.", settings.Maximum.Value], nameof(field.Value)); | ||
} | ||
|
||
// Check the number of decimals. | ||
if (Math.Round(field.Value.Value, settings.Scale) != field.Value.Value) | ||
{ | ||
if (settings.Scale == 0) | ||
{ | ||
context.Fail(S["The {0} field must be an integer.", context.ContentPartFieldDefinition.DisplayName()], nameof(field.Value)); | ||
} | ||
else | ||
{ | ||
context.Fail(S["Invalid number of digits for {0}, max allowed: {1}.", context.ContentPartFieldDefinition.DisplayName(), settings.Scale], nameof(field.Value)); | ||
} | ||
} | ||
} | ||
|
||
return Task.CompletedTask; | ||
} | ||
} |
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
30 changes: 30 additions & 0 deletions
30
src/OrchardCore.Modules/OrchardCore.ContentFields/Handlers/TimeFieldHandler.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,30 @@ | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Localization; | ||
using OrchardCore.ContentFields.Fields; | ||
using OrchardCore.ContentFields.Settings; | ||
using OrchardCore.ContentManagement.Handlers; | ||
using OrchardCore.ContentManagement.Metadata.Models; | ||
|
||
namespace OrchardCore.ContentFields.Handlers; | ||
|
||
public class TimeFieldHandler : ContentFieldHandler<TimeField> | ||
{ | ||
private readonly IStringLocalizer S; | ||
|
||
public TimeFieldHandler(IStringLocalizer<TimeFieldHandler> stringLocalizer) | ||
{ | ||
S = stringLocalizer; | ||
} | ||
|
||
public override Task ValidatingAsync(ValidateContentFieldContext context, TimeField field) | ||
{ | ||
var settings = context.ContentPartFieldDefinition.GetSettings<TimeFieldSettings>(); | ||
|
||
if (settings.Required && !field.Value.HasValue) | ||
{ | ||
context.Fail(S["A value is required for {0}.", context.ContentPartFieldDefinition.DisplayName()], nameof(field.Value)); | ||
} | ||
|
||
return Task.CompletedTask; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/OrchardCore.Modules/OrchardCore.ContentFields/Handlers/UserPickerFieldHandler.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,35 @@ | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Localization; | ||
using OrchardCore.ContentFields.Fields; | ||
using OrchardCore.ContentFields.Settings; | ||
using OrchardCore.ContentManagement.Handlers; | ||
using OrchardCore.ContentManagement.Metadata.Models; | ||
|
||
namespace OrchardCore.ContentFields.Handlers; | ||
|
||
public class UserPickerFieldHandler : ContentFieldHandler<UserPickerField> | ||
{ | ||
private readonly IStringLocalizer S; | ||
|
||
public UserPickerFieldHandler(IStringLocalizer<UserPickerFieldHandler> stringLocalizer) | ||
{ | ||
S = stringLocalizer; | ||
} | ||
|
||
public override Task ValidatingAsync(ValidateContentFieldContext context, UserPickerField field) | ||
{ | ||
var settings = context.ContentPartFieldDefinition.GetSettings<UserPickerFieldSettings>(); | ||
|
||
if (settings.Required && field.UserIds.Length == 0) | ||
{ | ||
context.Fail(S["The {0} field is required.", context.ContentPartFieldDefinition.DisplayName()], nameof(field.UserIds)); | ||
} | ||
|
||
if (!settings.Multiple && field.UserIds.Length > 1) | ||
{ | ||
context.Fail(S["The {0} field cannot contain multiple items.", context.ContentPartFieldDefinition.DisplayName()], nameof(field.UserIds)); | ||
} | ||
|
||
return Task.CompletedTask; | ||
} | ||
} |
Oops, something went wrong.