AddressValidator

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
10 years ago
Hi,

To add specific pin code deliveries only I have added Rule for Fluent Validation.

The problem is now all the addresses are validated against this rule. viz Billing, account address and shipping.

How can I make a check to add this model and validator only to shipping address. All other address should work as they are now.

Please help.

Rohit
10 years ago
Hi,

The simple solution is added at this moment. It is not a good way to do it as it must be through plugins. But at this moment not sure how to do it in plug-in.

\Presentation\Nop.Web\Models\Common\AddressModel.cs
1] Add one varibale -> isPinValidationRequired. Add on additional constructor for it.
2] Add constructor getting this as true. in default consturctor make it as false.

\Presentation\Nop.Web\Validators\Common\AddressValidator.cs
1]Add additional clause in zipPostalCode checking with Must to check all you pincodes. But When clause is also important with isPinValidationRequired.
2]  RuleFor(x => x.ZipPostalCode).NotEmpty().WithMessage(localizationService.GetResource("Account.Fields.ZipPostalCode.Required"));
RuleFor(x => x.ZipPostalCode).Must(checkcodes).When(x => x.isPinValidationRequired).WithMessage("Invalid Shipping pin"));

\Presentation\Nop.Web\Models\Checkout\CheckoutShippingAddressModel.cs
1] NewAddress = new AddressModel(true);

\Presentation\Nop.Web\Controllers\CheckoutController.cs
1] In existing address addition in PrepareShippingAddress you may restrict the address based on zip validation for shipping
foreach (var address in addresses)
            {
                var addressModel = new AddressModel();
                addressModel.PrepareModel(address,
                    false,
                    _addressSettings);
                if( Valid with address.zipPostalCode )
                model.ExistingAddresses.Add(addressModel);
            }


This solution is for people who wants to make only few changes  to nop.web.dll and shipping validation will work correctly in 1 hours time. Build the solution and change your Nop.Web.dll

If anyone can help us regarding how to take this code in plugin so that updates will be easier?

Rohit
10 years ago
It's not clear to me...
a) if by "validation" you just mean that zip(pin) is required, or whether it has to be in a list of valid pin codes
b) if you want to check the zip/pin code both when a "new" address is entered on the select shipping address page, and also if customer chooses an already existing address to use.

Shipping Director can handle validation by checking just before the shipping rate calculation is done. e.g. as per the first ErrorExit in this blog/example

But, I suppose that's not as friendly as while customer is entering a new address, but I would think it is basically same as if choosing an existing address. :)
10 years ago
Yes.

I need valid set of zip codes where we ship. The quick fix worked well.

I seen your website and shipping director product looks good.

Rohit
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.