Is there anyway I can override some validation rules for a particular class?

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
7 years ago
Is there anyway I can override some validation rules for a particular class?

For example add some validation rules for

public class AddressValidator : BaseNopValidator<AddressModel>

From within a plugin?
7 years ago
Anyone?
7 years ago
CarpeDiem wrote:
Is there anyway I can override some validation rules for a particular class?

For example add some validation rules for

public class AddressValidator : BaseNopValidator<AddressModel>

From within a plugin?


I've done AJAX overriding for some validation, I did look as it's interesting, but it looks like you'll need a new Nop.Web - but in BaseNopValidators.cs it does say


        /// <summary>
        /// Developers can override this method in custom partial classes
        /// in order to add some custom initialization code to constructors
        /// </summary>


It would be good to see an example of this?

thanks,
4 years ago
CarpeDiem wrote:
Is there anyway I can override some validation rules for a particular class?
For example add some validation rules for
public class AddressValidator : BaseNopValidator<AddressModel>
From within a plugin?

Is Anyone get luck in this via plugin?
4 years ago
I am also stuck in the same
The validation for a plugin for nopcommerce 4.2

My Class validator

namespace Nop.Plugin.Invoice.Validators
{
    /// <summary>
    /// Represents configuration model validator
    /// </summary>
    public class ConfigurationValidator : BaseNopValidator<ConfigurationModel>
    {
        #region Ctor
        public ConfigurationValidator(ILocalizationService localizationService)
        {
            //set validation rules
            RuleFor(model => model.Rfc).NotEmpty().WithMessage(localizationService.GetResource("Plugins.Invoice.Fields.Rfc.Required"));
        }
        #endregion
    }
}


My Model

namespace Nop.Plugin.Invoice.Models
{
    [Validator(typeof(ConfigurationValidator))]
    public class ConfigurationModel: BaseNopModel
    {
        [NopResourceDisplayName("Plugins.Invoice.Rfc")]
        public string Rfc { get; set; }      
    }
}


Someone is already using the BaseNopValidator in some plugin
4 years ago
Hi Your issue is seems different, like validation is not wroking in nopCommerce 4.20 plugin .

For this read this topic and here is solution of your problem.

You've to register validator class in ConfigureServices.
like
public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
        {
            //register validator
            services.AddTransient<IValidator<CustomRegisterModel>, RegisterValidator>();
        }
4 years ago
Thank Raju Paladiya

Its working now, add the line of code as you comment

//register validator
services.AddTransient<IValidator<ConfigurationModel>, ConfigurationValidator>();


but also, convert the shared validation class.


public partial class ConfigurationValidator : BaseNopValidator<ConfigurationModel>
    {
      ...
    }
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.