[Feature request] Validator created notification

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
5 years ago
Good day.

I'm creating a Post Russia shipping plugin and must validate the customer's delivery address. I did not find an easy way to include my validation without changing source. I’ve seen few questions about address validation in the forum, but there are not suitable answers.

As a result, I had to change the source:

1. Added notification publisher in NopValidatorFactory^


            var instance = EngineContext.Current.ResolveUnregistered(validatorAttribute.ValidatorType);

            if (instance != null)
            {
                try
                {
                    var eventPublisher = EngineContext.Current.Resolve<IEventPublisher>();

                    eventPublisher.ValidatorPrepare(type, instance);
                }
                catch
                {
                    // ignored
                }
            }


2. In the plugin implemented IConsumer<ValidatorPreparedEvent> and its handler:


        public void HandleEvent(ValidatorPreparedEvent eventMessage)
        {
            var isAddressModel = eventMessage.ModelType.FullName == Nop.Web.Models.Common.AddressModel";

            if (!isAddressModel)
                return;

            var typeInfo = eventMessage.Validator.GetType().GetTypeInfo();

            var methodInfo = typeInfo.GetMethod("AddRule");

            if (methodInfo != null)
            {
                var rule = new object[] {new AddressValidationRule()};

                methodInfo.Invoke(eventMessage.Validator, rule);
            }
        }



I use invoke because I don't want to create refference from plugin to Nop.Web - AddressModel is there.



This method does work. Could the team add something similar in the official code please? Changing the code is bad practics.

Thank you in advance,
Serge
5 years ago
Thanks for suggestion! We'll consider it - https://github.com/nopSolutions/nopCommerce/issues/3215
5 years ago
What kind of delivery address validation do you mean? What exactly do you want to validate, for example, correct input of fields or whether an address really exists?
5 years ago
ruslansuf wrote:
What exactly do you want to validate, for example, correct input of fields or whether an address really exists?


Well, really, this does not matter. I want to be able to add my own rules for any model validators. And validate whatever I want.
5 years ago
Please see method ValidatePaymentForm in src\Plugins\Nop.Plugin.Payments.Manual\ManualPaymentProcessor.cs. There implemented validator for credit card (PaymentInfoValidator).
5 years ago
ruslansuf wrote:
Please see method ValidatePaymentForm in src\Plugins\Nop.Plugin.Payments.Manual\ManualPaymentProcessor.cs. There implemented validator for credit card (PaymentInfoValidator).


But it can work because you make this call:


var warnings = paymentMethod.ValidatePaymentForm(form);


I've no found something like ValidateAddressForm.
5 years ago
SoftAIN wrote:
I've no found something like ValidateAddressForm.


And....there are few places where the address can be edited. For eample, in the user's account out of the checkout process. I would be nice to be able to validate the address in this place too.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.