Skip to content

Commit

Permalink
Required valie for form element name (#13557)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeAlhayek authored Apr 22, 2023
1 parent ea7e383 commit 4348db5
Showing 1 changed file with 15 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Localization;
using OrchardCore.ContentManagement.Display.ContentDisplay;
using OrchardCore.DisplayManagement.ModelBinding;
using OrchardCore.DisplayManagement.Views;
using OrchardCore.Forms.Models;
using OrchardCore.Forms.ViewModels;
using OrchardCore.Mvc.ModelBinding;

namespace OrchardCore.Forms.Drivers
{
public class FormInputElementPartDisplayDriver : ContentPartDisplayDriver<FormInputElementPart>
{
private readonly IStringLocalizer S;

public FormInputElementPartDisplayDriver(IStringLocalizer<FormInputElementPartDisplayDriver> stringLocalizer)
{
S = stringLocalizer;
}

public override IDisplayResult Edit(FormInputElementPart part)
{
return Initialize<FormInputElementPartEditViewModel>("FormInputElementPart_Fields_Edit", m =>
Expand All @@ -23,6 +33,11 @@ public async override Task<IDisplayResult> UpdateAsync(FormInputElementPart part

if (await updater.TryUpdateModelAsync(viewModel, Prefix))
{
if (String.IsNullOrWhiteSpace(viewModel.Name))
{
updater.ModelState.AddModelError(Prefix, nameof(viewModel.Name), S["A value is required for Name."]);
}

part.Name = viewModel.Name?.Trim();
part.ContentItem.DisplayText = part.Name;
}
Expand Down

0 comments on commit 4348db5

Please sign in to comment.