Skip to content

Commit

Permalink
Add field validation handlers to ensure field validation when content…
Browse files Browse the repository at this point in the history
…s are created outside the UI. Fix #12978 (#12979)
  • Loading branch information
MikeAlhayek authored Dec 29, 2022
1 parent e38076a commit 7c2f230
Show file tree
Hide file tree
Showing 16 changed files with 526 additions and 14 deletions.
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;
}
}

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;
}
}
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;
}
}
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;
}
}
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;
}
}
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;
}
}
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public override Task ValidatingAsync(ValidateContentFieldContext context, TextFi
{
var settings = context.ContentPartFieldDefinition.GetSettings<TextFieldSettings>();

if (settings.Required && String.IsNullOrEmpty(field.Text))
if (settings.Required && String.IsNullOrWhiteSpace(field.Text))
{
context.Fail(S["A value is required for {0}.", context.ContentPartFieldDefinition.DisplayName()], nameof(field.Text));
}
Expand Down
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;
}
}
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;
}
}
Loading

0 comments on commit 7c2f230

Please sign in to comment.