Hi,
A separate field will help query out the database for marketing purposes and not require validation logic like next.
Work around created with next code on validators in common and in customer. Admin has also some validation logic.
if (customerSettings.StreetAddressRequired && customerSettings.StreetAddressEnabled)
{
RuleFor(x => x.StreetAddress).NotEmpty().WithMessage(localizationService.GetResource("Account.Fields.StreetAddress.Required"));
//BEGIN QE: 20141008: Workitem 854
RuleFor(x => x.StreetAddress).Must(Nop.Web.Framework.QE.Validators.AddressValidatorQE.MustContainHouseNumber).WithMessage(localizationService.GetResource("Nop.Web.Framework.QE.Validators.Account.Fields.HouseNumber.Required"));
//END QE: 20141008: Workitem 854
}
Code for validators:
namespace Nop.Web.Framework.QE.Validators
{
public static class AddressValidatorQE
{
public static bool MustContainHouseNumber(string value)
{
return MustMatchPattern(value, @"\d+$");
}
public static bool MustMatchPattern(string value, string pattern)
{
if (string.IsNullOrEmpty(value))
return false;
Regex regex = new Regex(pattern);
return regex.IsMatch(value);
}
}
}
Language resource which can be loaded through Language Import functionality.
<?xml version="1.0" encoding="utf-8"?>
<Language Name="Nederlands">
<LocaleResource Name="Nop.Web.Framework.QE.Validators.Account.Fields.HouseNumber.Required">
<Value>Huisnummer ontbreekt</Value>
</LocaleResource>
</Language>
Tests (with VS unit test)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Nop.Core.Domain.Common;
using Nop.Web.Models.Common;
using Nop.Web.MVC.Tests.Public.Validators;
using Nop.Web.Validators.Common;
using FluentValidation.TestHelper;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Nop.Web.Framework.QE.Tests.Public.Validators.Common
{
[TestClass]
public class AddressValidatorTests : BaseValidatorTests
{
[TestMethod]
public void Should_have_error_when_streetaddress_has_no_housenumber()
{
var model = new AddressModel();
//required
var validator = new AddressValidator(_localizationService,
new AddressSettings()
{
StreetAddressEnabled = true,
StreetAddressRequired = true
});
model.Address1 = "Street address";
validator.ShouldHaveValidationErrorFor(x => x.Address1, model);
//not required
validator = new AddressValidator(_localizationService,
new AddressSettings()
{
StreetAddressEnabled = true,
StreetAddressRequired = false
});
model.Address1 = "Street address";
validator.ShouldNotHaveValidationErrorFor(x => x.Address1, model);
}
[TestMethod]
public void Should_not_have_error_when_streetaddress_has_housenumber()
{
var validator = new AddressValidator(_localizationService,
new AddressSettings()
{
StreetAddressEnabled = true
});
var model = new AddressModel();
model.Address1 = "Street address 41";
validator.ShouldNotHaveValidationErrorFor(x => x.Address1, model);
}
}
}