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

Template content validation #16944

Merged
merged 7 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -164,26 +164,7 @@ public async Task<IActionResult> CreatePost(TemplateViewModel model, string subm

if (ModelState.IsValid)
{
if (string.IsNullOrWhiteSpace(model.Name))
{
ModelState.AddModelError(nameof(TemplateViewModel.Name), S["The name is mandatory."]);
}
else if (string.IsNullOrWhiteSpace(model.Content))
{
ModelState.AddModelError(nameof(TemplateViewModel.Content), S["The content is mandatory."]);
}
else
{
var templatesDocument = model.AdminTemplates
? await _adminTemplatesManager.GetTemplatesDocumentAsync()
: await _templatesManager.GetTemplatesDocumentAsync()
;

if (templatesDocument.Templates.ContainsKey(model.Name))
{
ModelState.AddModelError(nameof(TemplateViewModel.Name), S["A template with the same name already exists."]);
}
}
await ValidateModelAsync(model);
}

if (ModelState.IsValid)
Expand Down Expand Up @@ -265,18 +246,7 @@ public async Task<IActionResult> Edit(string sourceName, TemplateViewModel model

if (ModelState.IsValid)
{
if (string.IsNullOrWhiteSpace(model.Name))
{
ModelState.AddModelError(nameof(TemplateViewModel.Name), S["The name is mandatory."]);
}
else if (!model.Name.Equals(sourceName, StringComparison.OrdinalIgnoreCase) && templatesDocument.Templates.ContainsKey(model.Name))
{
ModelState.AddModelError(nameof(TemplateViewModel.Name), S["A template with the same name already exists."]);
}
else if (string.IsNullOrWhiteSpace(model.Content))
{
ModelState.AddModelError(nameof(TemplateViewModel.Content), S["The content is mandatory."]);
}
await ValidateModelAsync(model, templatesDocument, sourceName);
}

if (!templatesDocument.Templates.ContainsKey(sourceName))
Expand Down Expand Up @@ -395,4 +365,32 @@ private IActionResult RedirectToReturnUrlOrIndex(string returnUrl)
return RedirectToAction(nameof(Index));
}
}

private async Task ValidateModelAsync(TemplateViewModel model, TemplatesDocument templatesDocument = null, string sourceName = null)
{
if (string.IsNullOrWhiteSpace(model.Name))
{
ModelState.AddModelError(nameof(TemplateViewModel.Name), S["The name is mandatory."]);
}
else
{
templatesDocument ??= model.AdminTemplates
? await _adminTemplatesManager.GetTemplatesDocumentAsync()
: await _templatesManager.GetTemplatesDocumentAsync();

if (!model.Name.Equals(sourceName, StringComparison.OrdinalIgnoreCase) &&
templatesDocument.Templates.ContainsKey(model.Name))
{
ModelState.AddModelError(nameof(TemplateViewModel.Name), S["A template with the same name already exists."]);
}
}

if (string.IsNullOrWhiteSpace(model.Content))
{
var placementsLink = Url.ActionLink("Index", "Admin", new { area = "OrchardCore.Placements" });
sarahelsaig marked this conversation as resolved.
Show resolved Hide resolved
var docsLink = "https://docs.orchardcore.net/en/main/reference/modules/Placements/";
sarahelsaig marked this conversation as resolved.
Show resolved Hide resolved
await _notifier.WarningAsync(H["If you left the content empty because you want to hide the shape, use <a href=\"{0}\">Placements</a> instead. See <a href=\"{1}\">the docs</a> for more info about this feature.", placementsLink, docsLink]);
ModelState.AddModelError(nameof(TemplateViewModel.Content), S["The content is mandatory."]);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
<label asp-for="Content" class="form-label">@T["Content"]</label>
<div id="@Html.IdFor(x => x.Content)_editor" asp-for="Text" style="min-height: 600px;" class="form-control" dir="@culture.GetLanguageDirection()"></div>
<textarea asp-for="Content" class="content-preview-text" hidden>@Html.Raw(Model.Content)</textarea>
<span asp-validation-for="Content"></span>
<span class="hint">@T["The Liquid template."]</span>
</div>
</form>
Expand Down