Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

Admin panel - Add validator for store model (URL fields)

KrzysztofPajak4bafa4bc0c6b
diff --git a/src/Web/Grand.Web.Admin/Validators/Stores/StoreValidator.cs b/src/Web/Grand.Web.Admin/Validators/Stores/StoreValidator.cs
index 630d62abc..84e303a14 100644
--- a/src/Web/Grand.Web.Admin/Validators/Stores/StoreValidator.cs
+++ b/src/Web/Grand.Web.Admin/Validators/Stores/StoreValidator.cs
@@ -3,6 +3,7 @@ using Grand.Infrastructure.Validators;
using Grand.Business.Common.Interfaces.Localization;
using Grand.Web.Admin.Models.Stores;
using System.Collections.Generic;
+using System;
namespace Grand.Web.Admin.Validators.Stores
{
@@ -16,6 +17,42 @@ namespace Grand.Web.Admin.Validators.Stores
RuleFor(x => x.Name).NotEmpty().WithMessage(translationService.GetResource("Admin.Configuration.Stores.Fields.Name.Required"));
RuleFor(x => x.Shortcut).NotEmpty().WithMessage(translationService.GetResource("Admin.Configuration.Stores.Fields.Shortcut.Required"));
RuleFor(x => x.Url).NotEmpty().WithMessage(translationService.GetResource("Admin.Configuration.Stores.Fields.Url.Required"));
+ RuleFor(x => x.Url).Must((x, y, context) =>
+ {
+ try
+ {
+ var storeUri = new Uri(x.Url);
+ return true;
+ }
+ catch
+ {
+ return false;
+ }
+ }).WithMessage(translationService.GetResource("Admin.Configuration.Stores.Fields.Url.WrongFormat"));
+ RuleFor(x => x.SecureUrl).Must((x, y, context) =>
+ {
+ try
+ {
+ if (!x.SslEnabled)
+ return true;
+
+ var sslUri = new Uri(x.SecureUrl);
+
+ if (!sslUri.Scheme.Equals("https", StringComparison.OrdinalIgnoreCase))
+ return false;
+
+ var storeUri = new Uri(x.Url);
+ if (sslUri.Host != storeUri.Host)
+ return false;
+
+ return true;
+ }
+ catch
+ {
+ return false;
+ }
+ }).WithMessage(translationService.GetResource("Admin.Configuration.Stores.Fields.SecureUrl.WrongFormat"));
+
}
}
}
\ No newline at end of file
diff --git a/src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xml b/src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xml
index 247c85814..3ed6f1aa4 100644
--- a/src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xml
+++ b/src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xml
@@ -5136,6 +5136,9 @@
<Resource Name="Admin.Configuration.Stores.Fields.SecureUrl">
<Value>Secure URL</Value>
</Resource>
+ <Resource Name="Admin.Configuration.Stores.Fields.SecureUrl.WrongFormat">
+ <Value>Wrong format of the secure URL (e.g. https://google.com) </Value>
+ </Resource>
<Resource Name="Admin.Configuration.Stores.Fields.Shortcut">
<Value>Shortcut</Value>
</Resource>
@@ -5154,6 +5157,9 @@
<Resource Name="Admin.Configuration.Stores.Fields.Url.Required">
<Value>URL of the store is required</Value>
</Resource>
+ <Resource Name="Admin.Configuration.Stores.Fields.Url.WrongFormat">
+ <Value>Wrong format of the URL (e.g. http://google.com) </Value>
+ </Resource>
<Resource Name="Admin.Configuration.Stores.Updated">
<Value>The store has been updated successfully.</Value>
</Resource>