Base solution for your next web application
Open Closed

V7 ICustomValidate not firing #7347


User avatar
0
exlnt created

In my application I have the entity shown below.

   public class CreateContactInput : ICustomValidate
    {
        [Required]
        public int Type { get; set; }

        [MaxLength(ContactConsts.EmailMaxLength)]
        public string Email { get; set; }

        [MaxLength(ContactConsts.PhoneMaxLength)]
        public string Phone { get; set; }
     
        [Required]
        [MaxLength(ContactConsts.ContactNameMaxLength)]
        public string ContactName { get; set; }
     
        private static bool IsValidEmailAddress(string emailAddress)
        {
            return new EmailAddressAttribute().IsValid(emailAddress);
        }

        private static bool IsValidPhoneNumber(string phoneNumber)
        {
            return new PhoneAttribute().IsValid(phoneNumber);
        }

        public void AddValidationErrors(CustomValidationContext context)
        {
            if (Type == 17 && string.IsNullOrEmpty(Email))
               context.Results.Add(new ValidationResult("Email address is required"));

            if (Type == 17 && !string.IsNullOrEmpty(Email))
            {
                if (!IsValidEmailAddress(Email))
                    context.Results.Add(new ValidationResult("Please enter a valid email address."));
            }

            if (Type == 18 && string.IsNullOrEmpty(Phone))
                context.Results.Add(new ValidationResult("Phone number is required."));

            if (Type == 18 && !string.IsNullOrEmpty(Phone))
            {
                if (!IsValidPhoneNumber(Phone))
                    context.Results.Add(new ValidationResult("Please enter a valid phone number."));
            }
        }
    }

I was using IValidatableObject interface before I added ICustomValidate today. However neither of these interfaces is firing the validation and its allowing the creation of a contact record with invalid email addresses?

I read ABP validation document.

I also found this thread on support forum, which seems to report the same issue.

Can someone please help me with this issue?


11 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team

    Please share the code for the application service method associated with CreateContactInput.

  • User Avatar
    0
    exlnt created

    Here you go.

            public async Task<long> CreateContact(CreateContactInput input)
            {
                var contact = ObjectMapper.Map<Contact>(input);
                if (AbpSession.TenantId != null)
                {
                    contact.TenantId = (int)AbpSession.TenantId;
                }
                var newId = await _contactRepository.InsertAndGetIdAsync(contact);
                return newId;
            }
    
  • User Avatar
    0
    maliming created
    Support Team

    @exlnt

    Is it convenient to share your project source code? Or you can try to reproduce it using Zero's Demo project.

    [email protected]

  • User Avatar
    0
    exlnt created

    I found a workaround. I was able to solve my issue by adding a RegEx validation attribute.

            [MaxLength(ContactConsts.EmailMaxLength)]
            [RegularExpression(CommonRegexFormats.Valid_Email, ErrorMessage = CommonRegexFormats.Valid_Email_ErrorMsg)]
            public string Email { get; set; }
    
            [MaxLength(ContactConsts.PhoneMaxLength)]
            [RegularExpression(CommonRegexFormats.Letters_Numbers_Space_Dash, ErrorMessage = "Phone number " + CommonRegexFormats.Letters_Numbers_Space_Dash_ErrorMsg)]
            public string Phone { get; set; }
    

    One related question, is there any way for me to localize the error messages on my RegEx attributes shown above? I tried to use L("MyString") but it does not recognize the "L", due to reference errors. Is it ok to do in the validaton constants classes?

  • User Avatar
    0
    maliming created
    Support Team

    RegularExpression seems to be unable to use the L method. We should find a way to solve the problem of ICustomValidate.

    Is it convenient to share your project source code? Or you can try to reproduce it using Zero's Demo project. [email protected]

  • User Avatar
    0
    exlnt created

    @maliming - Yes my repo is on GITHUB. You can see the issue on this repo. I have granted you access to the repo. Please clone and test on the branch "V7Upgrade".

  • User Avatar
    0
    maliming created
    Support Team

    hi @exlnt

    I used the ICustomValidate interface and called createContact on the front end, which can call the AddValidationErrors method.

  • User Avatar
    0
    exlnt created

    The basic required field validation does work. It's the valid email address portion of the email address that is not working. Test it again and this time just enter a simple word for the email field and hit submit.

  • User Avatar
    0
    maliming created
    Support Team

    You should use Zero's built-in mail validation method.

    ValidationHelper.IsEmail(yourEmail)

  • User Avatar
    0
    exlnt created

    @maliming - Can you please share the documentation for that helper? I was not able to find it anywhere.

  • User Avatar
    0
    exlnt created

    Ignore my previous message. I found the example in the user DTO class.

            public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
            {
                if (!UserName.IsNullOrEmpty())
                {
                    if (!UserName.Equals(EmailAddress, StringComparison.OrdinalIgnoreCase) && ValidationHelper.IsEmail(UserName))
                    {
                        yield return new ValidationResult("Username cannot be an email address unless it's same with your email address !");
                    }
                }
            }