Skip to content

Commit

Permalink
Admin panel - Add validator for store model (URL fields)
Browse files Browse the repository at this point in the history
  • Loading branch information
KrzysztofPajak committed Sep 1, 2021
1 parent 0e1f0be commit 4bafa4b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/Web/Grand.Web.Admin/Validators/Stores/StoreValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Grand.Business.Common.Interfaces.Localization;
using Grand.Web.Admin.Models.Stores;
using System.Collections.Generic;
using System;

namespace Grand.Web.Admin.Validators.Stores
{
Expand All @@ -16,6 +17,42 @@ public class StoreValidator : BaseGrandValidator<StoreModel>
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"));

}
}
}
6 changes: 6 additions & 0 deletions src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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>
Expand Down

0 comments on commit 4bafa4b

Please sign in to comment.