Skip to content

Commit

Permalink
Added page internal references management in LinkField valid urls (e.…
Browse files Browse the repository at this point in the history
…g. field.Value = "#someId")
  • Loading branch information
AndreaPiovanelli committed Oct 14, 2024
1 parent 0d9fccb commit 54afc43
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
25 changes: 22 additions & 3 deletions src/Orchard.Web/Modules/Orchard.Fields/Drivers/LinkFieldDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,30 @@ protected override DriverResult Editor(ContentPart part, LinkField field, IUpdat
if (settings.Required && String.IsNullOrWhiteSpace(field.Value)) {
updater.AddModelError(GetPrefix(field, part), T("Url is required for {0}", T(field.DisplayName)));
}
else if (!String.IsNullOrWhiteSpace(field.Value) && !Uri.IsWellFormedUriString(field.Value, UriKind.RelativeOrAbsolute)) {
updater.AddModelError(GetPrefix(field, part), T("{0} is an invalid url.", field.Value));
}
else if (settings.LinkTextMode == LinkTextMode.Required && String.IsNullOrWhiteSpace(field.Text)) {
updater.AddModelError(GetPrefix(field, part), T("Text is required for {0}.", T(field.DisplayName)));
} else if (!String.IsNullOrWhiteSpace(field.Value)) {
// Check if it's a valid uri, considering that there may be the link to an anchor only
// e.g.: field.Value = "#divId"
// Take everything before the first "#" character and check if it's a valid uri.
// If there is no character before the first "#", consider the value as a valid one (because it is a reference to a div inside the same page)
var index = field.Value.IndexOf('#');
if (index >= 0) {
var url = field.Value.Substring(0, index);
if (!String.IsNullOrWhiteSpace(url) && !Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute)) {
updater.AddModelError(GetPrefix(field, part), T("{0} is an invalid url.", field.Value));
} else {
// The first part of the value is valid (or empty)
// In the same way, check that the second part of the value (after the "#" character) is valid
// For html 5, a tag id is valid as long as it doesn't contain white spaces
var anchor = field.Value.Substring(index + 1);
if (anchor.IndexOf(' ') >= 0) {
updater.AddModelError(GetPrefix(field, part), T("{0} is an invalid url.", field.Value));
}
}
} else if (!Uri.IsWellFormedUriString(field.Value, UriKind.RelativeOrAbsolute)) {
updater.AddModelError(GetPrefix(field, part), T("{0} is an invalid url.", field.Value));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
</div>
<div class="editor-field">
@(settings.Required ? Html.TextBoxFor(m => m.Value, new { @class = "text large", required = "required" }) : Html.TextBoxFor(m => m.Value, new { @class = "text large" }))
<span class="hint">@T("A valid url, i.e. http://orchardproject.net, /content/file.pdf, ...")</span>
<span class="hint">@T("A valid url, i.e. http://orchardproject.net, /content/file.pdf, #some_id, ...")</span>
</div>
@if (settings.LinkTextMode == LinkTextMode.Optional || settings.LinkTextMode == LinkTextMode.Required) {
<div class="editor-label">
Expand Down

0 comments on commit 54afc43

Please sign in to comment.