Extend validation for existing tables

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
7 năm cách đây
Hi dear.
I want to add extra RuleFor for Phone Field in RegisterModel in custom plugin.so I create a plugin and extend RegisterValidator The following form:

    public partial class RegisterValidator : AbstractValidator<RegisterModel>
    {
        public RegisterValidator(ILocalizationService localizationService)
        {
            RuleFor(x => x.Phone)
           .Matches(
@"^(0|\+98)?([ ]|,|-|[()]){0,2}9[1|2|3|4]([ ]|,|-|[()]){0,2}(?:[0-9]([ ]|,|-|[()]){0,2}){8}$")
           .WithMessage(localizationService.GetResource("Account.Fields.Phone.InvalidMobileNumber"));
        }
    }
but this not work.

Generally, how can change RegisterValidator in custom plugin?!

Please advice me,if possible.
[Best Regards]
7 năm cách đây
Don't bother doing this. Partial classes is not available cross-project. :)
7 năm cách đây
Thanks for your reply.

But,what's the solution for this issue.

My problem is that,I want to change email field to optional and add another Rule Validation to Phone Field.


CustomerController => RegisterModel => RegisterValidator


Please help me.
7 năm cách đây
As far as I can see, you cannot override the class.

What you can do though, is to remove all the validation before ModelState.IsValid, and apply your own in controller. Look at how PaymentController validates and apply the same trick:

        [NonAction]
        public override IList<string> ValidatePaymentForm(FormCollection form)
        {
            var warnings = new List<string>();

            //validate
            var validator = new PaymentInfoValidator(_localizationService);
            var model = new PaymentInfoModel
            {
                CardholderName = form["CardholderName"],
                CardNumber = form["CardNumber"],
                CardCode = form["CardCode"],
                ExpireMonth = form["ExpireMonth"],
                ExpireYear = form["ExpireYear"]
            };
            var validationResult = validator.Validate(model);
            if (!validationResult.IsValid)
                foreach (var error in validationResult.Errors)
                    warnings.Add(error.ErrorMessage);
            return warnings;
        }
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.