Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display Drivers validation issue #15488

Merged
merged 11 commits into from
Apr 18, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public override async Task<IDisplayResult> EditAsync(AdminSettings settings, Bui
}).Location("Content:3").OnGroup(GroupId);
}

public override async Task<IDisplayResult> UpdateAsync(AdminSettings settings, BuildEditorContext context)
public override async Task<IDisplayResult> UpdateAsync(AdminSettings settings, UpdateEditorContext context)
{
var user = _httpContextAccessor.HttpContext?.User;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,18 @@ public override IDisplayResult Edit(LinkAdminNode treeNode)
public override async Task<IDisplayResult> UpdateAsync(LinkAdminNode treeNode, IUpdateModel updater)
{
var model = new LinkAdminNodeViewModel();
if (await updater.TryUpdateModelAsync(model, Prefix, x => x.LinkUrl, x => x.LinkText, x => x.IconClass, x => x.SelectedPermissionNames))
{
treeNode.LinkText = model.LinkText;
treeNode.LinkUrl = model.LinkUrl;
treeNode.IconClass = model.IconClass;
await updater.TryUpdateModelAsync(model, Prefix, x => x.LinkUrl, x => x.LinkText, x => x.IconClass, x => x.SelectedPermissionNames);

var selectedPermissions = (model.SelectedPermissionNames == null ? [] : model.SelectedPermissionNames.Split(',', StringSplitOptions.RemoveEmptyEntries));
treeNode.LinkText = model.LinkText;
treeNode.LinkUrl = model.LinkUrl;
treeNode.IconClass = model.IconClass;

var permissions = await _adminMenuPermissionService.GetPermissionsAsync();
treeNode.PermissionNames = permissions
.Where(p => selectedPermissions.Contains(p.Name))
.Select(p => p.Name).ToArray();
}
var selectedPermissions = (model.SelectedPermissionNames == null ? [] : model.SelectedPermissionNames.Split(',', StringSplitOptions.RemoveEmptyEntries));

var permissions = await _adminMenuPermissionService.GetPermissionsAsync();
treeNode.PermissionNames = permissions
.Where(p => selectedPermissions.Contains(p.Name))
.Select(p => p.Name).ToArray();

return Edit(treeNode);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,16 @@ public override IDisplayResult Edit(PlaceholderAdminNode treeNode)
public override async Task<IDisplayResult> UpdateAsync(PlaceholderAdminNode treeNode, IUpdateModel updater)
{
var model = new PlaceholderAdminNodeViewModel();
if (await updater.TryUpdateModelAsync(model, Prefix, x => x.LinkText, x => x.IconClass, x => x.SelectedPermissionNames))
{
treeNode.LinkText = model.LinkText;
treeNode.IconClass = model.IconClass;
await updater.TryUpdateModelAsync(model, Prefix, x => x.LinkText, x => x.IconClass, x => x.SelectedPermissionNames);

var selectedPermissions = (model.SelectedPermissionNames == null ? Array.Empty<string>() : model.SelectedPermissionNames.Split(',', StringSplitOptions.RemoveEmptyEntries));
var permissions = await _adminMenuPermissionService.GetPermissionsAsync();
treeNode.PermissionNames = permissions
.Where(p => selectedPermissions.Contains(p.Name))
.Select(p => p.Name).ToArray();
}
treeNode.LinkText = model.LinkText;
treeNode.IconClass = model.IconClass;

var selectedPermissions = (model.SelectedPermissionNames == null ? Array.Empty<string>() : model.SelectedPermissionNames.Split(',', StringSplitOptions.RemoveEmptyEntries));
var permissions = await _adminMenuPermissionService.GetPermissionsAsync();
treeNode.PermissionNames = permissions
.Where(p => selectedPermissions.Contains(p.Name))
.Select(p => p.Name).ToArray();

return Edit(treeNode);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,15 @@ public override async Task<IDisplayResult> UpdateAsync(ContentTypePartDefinition
{
var model = new AliasPartSettingsViewModel();

if (await context.Updater.TryUpdateModelAsync(model, Prefix, m => m.Pattern, m => m.Options))
await context.Updater.TryUpdateModelAsync(model, Prefix, m => m.Pattern, m => m.Options);

if (!string.IsNullOrEmpty(model.Pattern) && !_templateManager.Validate(model.Pattern, out var errors))
{
context.Updater.ModelState.AddModelError(nameof(model.Pattern), S["Pattern doesn't contain a valid Liquid expression. Details: {0}", string.Join(" ", errors)]);
}
else
{
if (!string.IsNullOrEmpty(model.Pattern) && !_templateManager.Validate(model.Pattern, out var errors))
{
context.Updater.ModelState.AddModelError(nameof(model.Pattern), S["Pattern doesn't contain a valid Liquid expression. Details: {0}", string.Join(" ", errors)]);
}
else
{
context.Builder.WithSettings(new AliasPartSettings { Pattern = model.Pattern, Options = model.Options });
}
context.Builder.WithSettings(new AliasPartSettings { Pattern = model.Pattern, Options = model.Options });
}

return Edit(contentTypePartDefinition, context.Updater);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public override async Task<IDisplayResult> EditAsync(AuditTrailSettings settings
}).Location("Content:1#Events").OnGroup(AuditTrailSettingsGroup.Id);
}

public override async Task<IDisplayResult> UpdateAsync(AuditTrailSettings settings, BuildEditorContext context)
public override async Task<IDisplayResult> UpdateAsync(AuditTrailSettings settings, UpdateEditorContext context)
{
if (!await _authorizationService.AuthorizeAsync(_httpContextAccessor.HttpContext?.User, AuditTrailPermissions.ManageAuditTrailSettings))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public override async Task<IDisplayResult> EditAsync(AuditTrailTrimmingSettings
}).Location("Content:10#Trimming;0").OnGroup(AuditTrailSettingsGroup.Id);
}

public override async Task<IDisplayResult> UpdateAsync(AuditTrailTrimmingSettings section, BuildEditorContext context)
public override async Task<IDisplayResult> UpdateAsync(AuditTrailTrimmingSettings section, UpdateEditorContext context)
{
var user = _httpContextAccessor.HttpContext?.User;
if (!await _authorizationService.AuthorizeAsync(user, AuditTrailPermissions.ManageAuditTrailSettings))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,12 @@ public override IDisplayResult Edit(DateField field, BuildFieldEditorContext con

public override async Task<IDisplayResult> UpdateAsync(DateField field, IUpdateModel updater, UpdateFieldEditorContext context)
{
if (await updater.TryUpdateModelAsync(field, Prefix, f => f.Value))
await updater.TryUpdateModelAsync(field, Prefix, f => f.Value);
var settings = context.PartFieldDefinition.GetSettings<DateFieldSettings>();

if (settings.Required && field.Value == null)
{
var settings = context.PartFieldDefinition.GetSettings<DateFieldSettings>();
if (settings.Required && field.Value == null)
{
updater.ModelState.AddModelError(Prefix, nameof(field.Value), S["A value is required for {0}.", context.PartFieldDefinition.DisplayName()]);
}
updater.ModelState.AddModelError(Prefix, nameof(field.Value), S["A value is required for {0}.", context.PartFieldDefinition.DisplayName()]);
}

return Edit(field, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,22 @@ public override async Task<IDisplayResult> UpdateAsync(DateTimeField field, IUpd
{
var model = new EditDateTimeFieldViewModel();

if (await updater.TryUpdateModelAsync(model, Prefix, f => f.LocalDateTime))
await updater.TryUpdateModelAsync(model, Prefix, f => f.LocalDateTime);
var settings = context.PartFieldDefinition.GetSettings<DateTimeFieldSettings>();

if (settings.Required && model.LocalDateTime == null)
{
updater.ModelState.AddModelError(Prefix, nameof(model.LocalDateTime), S["A value is required for {0}.", context.PartFieldDefinition.DisplayName()]);
}
else
{
var settings = context.PartFieldDefinition.GetSettings<DateTimeFieldSettings>();
if (settings.Required && model.LocalDateTime == null)
if (model.LocalDateTime == null)
{
updater.ModelState.AddModelError(Prefix, nameof(model.LocalDateTime), S["A value is required for {0}.", context.PartFieldDefinition.DisplayName()]);
field.Value = null;
}
else
{
if (model.LocalDateTime == null)
{
field.Value = null;
}
else
{
field.Value = await _localClock.ConvertToUtcAsync(model.LocalDateTime.Value);
}
field.Value = await _localClock.ConvertToUtcAsync(model.LocalDateTime.Value);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,22 +84,20 @@ public override async Task<IDisplayResult> UpdateAsync(HtmlField field, IUpdateM
var viewModel = new EditHtmlFieldViewModel();

var settings = context.PartFieldDefinition.GetSettings<HtmlFieldSettings>();
await updater.TryUpdateModelAsync(viewModel, Prefix, f => f.Html);

if (await updater.TryUpdateModelAsync(viewModel, Prefix, f => f.Html))
if (!string.IsNullOrEmpty(viewModel.Html) && !_liquidTemplateManager.Validate(viewModel.Html, out var errors))
{
if (!string.IsNullOrEmpty(viewModel.Html) && !_liquidTemplateManager.Validate(viewModel.Html, out var errors))
{
var fieldName = context.PartFieldDefinition.DisplayName();
context.Updater.ModelState.AddModelError(
Prefix,
nameof(viewModel.Html), S["{0} doesn't contain a valid Liquid expression. Details: {1}",
fieldName,
string.Join(' ', errors)]);
}
else
{
field.Html = settings.SanitizeHtml ? _htmlSanitizerService.Sanitize(viewModel.Html) : viewModel.Html;
}
var fieldName = context.PartFieldDefinition.DisplayName();
context.Updater.ModelState.AddModelError(
Prefix,
nameof(viewModel.Html), S["{0} doesn't contain a valid Liquid expression. Details: {1}",
fieldName,
string.Join(' ', errors)]);
}
else
{
field.Html = settings.SanitizeHtml ? _htmlSanitizerService.Sanitize(viewModel.Html) : viewModel.Html;
}

return Edit(field, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,14 @@ public override IDisplayResult Edit(MultiTextField field, BuildFieldEditorContex
public override async Task<IDisplayResult> UpdateAsync(MultiTextField field, IUpdateModel updater, UpdateFieldEditorContext context)
{
var viewModel = new EditMultiTextFieldViewModel();
if (await updater.TryUpdateModelAsync(viewModel, Prefix))
{
field.Values = viewModel.Values;
await updater.TryUpdateModelAsync(viewModel, Prefix);

var settings = context.PartFieldDefinition.GetSettings<MultiTextFieldSettings>();
if (settings.Required && viewModel.Values.Length == 0)
{
updater.ModelState.AddModelError(Prefix, nameof(field.Values), S["A value is required for {0}.", context.PartFieldDefinition.DisplayName()]);
}
field.Values = viewModel.Values;

var settings = context.PartFieldDefinition.GetSettings<MultiTextFieldSettings>();
if (settings.Required && viewModel.Values.Length == 0)
{
updater.ModelState.AddModelError(Prefix, nameof(field.Values), S["A value is required for {0}.", context.PartFieldDefinition.DisplayName()]);
}

return Edit(field, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,48 +67,46 @@ public override async Task<IDisplayResult> UpdateAsync(NumericField field, IUpda
{
var viewModel = new EditNumericFieldViewModel();

if (await updater.TryUpdateModelAsync(viewModel, Prefix, f => f.Value))
{
var settings = context.PartFieldDefinition.GetSettings<NumericFieldSettings>();
await updater.TryUpdateModelAsync(viewModel, Prefix, f => f.Value);
var settings = context.PartFieldDefinition.GetSettings<NumericFieldSettings>();

field.Value = null;
field.Value = null;

if (string.IsNullOrWhiteSpace(viewModel.Value))
if (string.IsNullOrWhiteSpace(viewModel.Value))
{
if (settings.Required)
{
if (settings.Required)
{
updater.ModelState.AddModelError(Prefix, nameof(field.Value), S["A value is required for {0}.", context.PartFieldDefinition.DisplayName()]);
}
updater.ModelState.AddModelError(Prefix, nameof(field.Value), S["A value is required for {0}.", context.PartFieldDefinition.DisplayName()]);
}
else if (!decimal.TryParse(viewModel.Value, NumberStyles.Any, CultureInfo.CurrentUICulture, out var value))
}
else if (!decimal.TryParse(viewModel.Value, NumberStyles.Any, CultureInfo.CurrentUICulture, out var value))
{
updater.ModelState.AddModelError(Prefix, nameof(field.Value), S["{0} is an invalid number.", context.PartFieldDefinition.DisplayName()]);
}
else
{
field.Value = value;

if (settings.Minimum.HasValue && value < settings.Minimum.Value)
{
updater.ModelState.AddModelError(Prefix, nameof(field.Value), S["{0} is an invalid number.", context.PartFieldDefinition.DisplayName()]);
updater.ModelState.AddModelError(Prefix, nameof(field.Value), S["The value must be greater than {0}.", settings.Minimum.Value]);
}
else
{
field.Value = value;

if (settings.Minimum.HasValue && value < settings.Minimum.Value)
{
updater.ModelState.AddModelError(Prefix, nameof(field.Value), S["The value must be greater than {0}.", settings.Minimum.Value]);
}
if (settings.Maximum.HasValue && value > settings.Maximum.Value)
{
updater.ModelState.AddModelError(Prefix, nameof(field.Value), S["The value must be less than {0}.", settings.Maximum.Value]);
}

if (settings.Maximum.HasValue && value > settings.Maximum.Value)
// Check the number of decimals.
if (Math.Round(value, settings.Scale) != value)
{
if (settings.Scale == 0)
{
updater.ModelState.AddModelError(Prefix, nameof(field.Value), S["The value must be less than {0}.", settings.Maximum.Value]);
updater.ModelState.AddModelError(Prefix, nameof(field.Value), S["The {0} field must be an integer.", context.PartFieldDefinition.DisplayName()]);
}

// Check the number of decimals.
if (Math.Round(value, settings.Scale) != value)
else
{
if (settings.Scale == 0)
{
updater.ModelState.AddModelError(Prefix, nameof(field.Value), S["The {0} field must be an integer.", context.PartFieldDefinition.DisplayName()]);
}
else
{
updater.ModelState.AddModelError(Prefix, nameof(field.Value), S["Invalid number of digits for {0}, max allowed: {1}.", context.PartFieldDefinition.DisplayName(), settings.Scale]);
}
updater.ModelState.AddModelError(Prefix, nameof(field.Value), S["Invalid number of digits for {0}, max allowed: {1}.", context.PartFieldDefinition.DisplayName(), settings.Scale]);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,12 @@ public override IDisplayResult Edit(TextField field, BuildFieldEditorContext con

public override async Task<IDisplayResult> UpdateAsync(TextField field, IUpdateModel updater, UpdateFieldEditorContext context)
{
if (await updater.TryUpdateModelAsync(field, Prefix, f => f.Text))
await updater.TryUpdateModelAsync(field, Prefix, f => f.Text);
var settings = context.PartFieldDefinition.GetSettings<TextFieldSettings>();

if (settings.Required && string.IsNullOrWhiteSpace(field.Text))
{
var settings = context.PartFieldDefinition.GetSettings<TextFieldSettings>();
if (settings.Required && string.IsNullOrWhiteSpace(field.Text))
{
updater.ModelState.AddModelError(Prefix, nameof(field.Text), S["A value is required for {0}.", context.PartFieldDefinition.DisplayName()]);
}
updater.ModelState.AddModelError(Prefix, nameof(field.Text), S["A value is required for {0}.", context.PartFieldDefinition.DisplayName()]);
}

return Edit(field, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,12 @@ public override IDisplayResult Edit(TimeField field, BuildFieldEditorContext con

public override async Task<IDisplayResult> UpdateAsync(TimeField field, IUpdateModel updater, UpdateFieldEditorContext context)
{
if (await updater.TryUpdateModelAsync(field, Prefix, f => f.Value))
await updater.TryUpdateModelAsync(field, Prefix, f => f.Value);
var settings = context.PartFieldDefinition.GetSettings<TimeFieldSettings>();

if (settings.Required && field.Value == null)
{
var settings = context.PartFieldDefinition.GetSettings<TimeFieldSettings>();
if (settings.Required && field.Value == null)
{
updater.ModelState.AddModelError(Prefix, nameof(field.Value), S["A value is required for {0}.", context.PartFieldDefinition.DisplayName()]);
}
updater.ModelState.AddModelError(Prefix, nameof(field.Value), S["A value is required for {0}.", context.PartFieldDefinition.DisplayName()]);
}

return Edit(field, context);
Expand Down
Loading