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.

Fix validators

KrzysztofPajak3d4d6dced7dc
diff --git a/src/Web/Grand.Web.Admin/Validators/Customers/CustomerValidator.cs b/src/Web/Grand.Web.Admin/Validators/Customers/CustomerValidator.cs
index 114e16eef..70a84f3d0 100644
--- a/src/Web/Grand.Web.Admin/Validators/Customers/CustomerValidator.cs
+++ b/src/Web/Grand.Web.Admin/Validators/Customers/CustomerValidator.cs
@@ -36,7 +36,7 @@ namespace Grand.Web.Admin.Validators.Customers
{
var countryId = !string.IsNullOrEmpty(x.CountryId) ? x.CountryId : "";
var country = await countryService.GetCountryById(countryId);
- if (country.StateProvinces.Any())
+ if (country != null && country.StateProvinces.Any())
{
//if yes, then ensure that state is selected
if (string.IsNullOrEmpty(y))
diff --git a/src/Web/Grand.Web/Validators/Common/AddressValidator.cs b/src/Web/Grand.Web/Validators/Common/AddressValidator.cs
index ab2f1580d..2f2546431 100644
--- a/src/Web/Grand.Web/Validators/Common/AddressValidator.cs
+++ b/src/Web/Grand.Web/Validators/Common/AddressValidator.cs
@@ -46,7 +46,7 @@ namespace Grand.Web.Validators.Common
{
var countryId = !string.IsNullOrEmpty(x.CountryId) ? x.CountryId : "";
var country = await countryService.GetCountryById(countryId);
- if (country.StateProvinces.Any())
+ if (country != null && country.StateProvinces.Any())
{
//if yes, then ensure that state is selected
if (String.IsNullOrEmpty(y))
@@ -81,7 +81,7 @@ namespace Grand.Web.Validators.Common
}
if (addressSettings.CityRequired && addressSettings.CityEnabled)
{
- RuleFor(x => x.City).NotEmpty().WithMessage(translationService.GetResource("Account.Fields.City.Required"));
+ RuleFor(x => x.City).NotEmpty().WithMessage(translationService.GetResource("Account.Fields.City.Required"));
}
if (addressSettings.PhoneRequired && addressSettings.PhoneEnabled)
{
diff --git a/src/Web/Grand.Web/Validators/Customer/CustomerInfoValidator.cs b/src/Web/Grand.Web/Validators/Customer/CustomerInfoValidator.cs
index d66fb9b71..c86993d7f 100644
--- a/src/Web/Grand.Web/Validators/Customer/CustomerInfoValidator.cs
+++ b/src/Web/Grand.Web/Validators/Customer/CustomerInfoValidator.cs
@@ -8,6 +8,7 @@ using Grand.Web.Models.Customer;
using System;
using System.Collections.Generic;
using Grand.SharedKernel.Extensions;
+using System.Linq;
namespace Grand.Web.Validators.Customer
{
@@ -50,18 +51,20 @@ namespace Grand.Web.Validators.Customer
{
RuleFor(x => x.StateProvinceId).MustAsync(async (x, y, context) =>
{
- //does selected country has states?
- var countryId = !String.IsNullOrEmpty(x.CountryId) ? x.CountryId : "";
- var hasStates = (await countryService.GetCountryById(countryId))?.StateProvinces.Count > 0;
- if (hasStates)
+ var countryId = !string.IsNullOrEmpty(x.CountryId) ? x.CountryId : "";
+ var country = await countryService.GetCountryById(countryId);
+ if (country != null && country.StateProvinces.Any())
{
//if yes, then ensure that state is selected
- if (String.IsNullOrEmpty(y))
+ if (string.IsNullOrEmpty(y))
{
return false;
}
+ if (country.StateProvinces.FirstOrDefault(x => x.Id == y) != null)
+ return true;
}
- return true;
+ return false;
+
}).WithMessage(translationService.GetResource("Account.Fields.StateProvince.Required"));
}
if (customerSettings.DateOfBirthEnabled && customerSettings.DateOfBirthRequired)
diff --git a/src/Web/Grand.Web/Validators/Customer/RegisterValidator.cs b/src/Web/Grand.Web/Validators/Customer/RegisterValidator.cs
index 658dd6e44..a9c2101ba 100644
--- a/src/Web/Grand.Web/Validators/Customer/RegisterValidator.cs
+++ b/src/Web/Grand.Web/Validators/Customer/RegisterValidator.cs
@@ -60,17 +60,20 @@ namespace Grand.Web.Validators.Customer
RuleFor(x => x.StateProvinceId).MustAsync(async (x, y, context) =>
{
//does selected country has states?
- var countryId = !String.IsNullOrEmpty(x.CountryId) ? x.CountryId : "";
- var hasStates = (await countryService.GetCountryById(countryId))?.StateProvinces.Count > 0;
- if (hasStates)
+ var countryId = !string.IsNullOrEmpty(x.CountryId) ? x.CountryId : "";
+ var country = await countryService.GetCountryById(countryId);
+ if (country != null && country.StateProvinces.Any())
{
//if yes, then ensure that state is selected
- if (String.IsNullOrEmpty(y))
+ if (string.IsNullOrEmpty(y))
{
return false;
}
+ if (country.StateProvinces.FirstOrDefault(x => x.Id == y) != null)
+ return true;
}
- return true;
+ return false;
+
}).WithMessage(translationService.GetResource("Account.Fields.StateProvince.Required"));
}
if (customerSettings.DateOfBirthEnabled && customerSettings.DateOfBirthRequired)
diff --git a/src/Web/Grand.Web/Validators/Vendors/VendorAddressValidator.cs b/src/Web/Grand.Web/Validators/Vendors/VendorAddressValidator.cs
index 7fcb2ee08..d58175841 100644
--- a/src/Web/Grand.Web/Validators/Vendors/VendorAddressValidator.cs
+++ b/src/Web/Grand.Web/Validators/Vendors/VendorAddressValidator.cs
@@ -31,18 +31,20 @@ namespace Grand.Web.Validators.Common
{
RuleFor(x => x.StateProvinceId).MustAsync(async (x, y, context) =>
{
- //does selected country has states?
- var countryId = !String.IsNullOrEmpty(x.CountryId) ? x.CountryId : "";
- var hasStates = (await countryServices.GetCountryById(countryId))?.StateProvinces.Count > 0;
- if (hasStates)
+ var countryId = !string.IsNullOrEmpty(x.CountryId) ? x.CountryId : "";
+ var country = await countryService.GetCountryById(countryId);
+ if (country != null && country.StateProvinces.Any())
{
//if yes, then ensure that state is selected
- if (String.IsNullOrEmpty(y))
+ if (string.IsNullOrEmpty(y))
{
return false;
}
+ if (country.StateProvinces.FirstOrDefault(x => x.Id == y) != null)
+ return true;
}
- return true;
+ return false;
+
}).WithMessage(translationService.GetResource("Account.VendorInfo.StateProvince.Required"));
}
if (addressSettings.CompanyRequired && addressSettings.CompanyEnabled)