Unable to pass Parameter Value in CustomValidation Attribute

Indudhar Gowda 426 Reputation points
2020-06-21T07:02:15.607+00:00

Unable to pass Parameter Value in CustomValidation Attribute

 public string Test { get; set; } = "ValidateSpecialCharacters";  
  
         [CustomValidation(typeof(ValidationTextBox), Test)]  
        public string GeneralDescription  
        {  
            get => _generalDescription;  
            set  
            {  
                SetValue(ref _generalDescription, value);  
                TestBorderBrush = ValidationDictionaryCollection["GeneralDescription"] != string.Empty  
                    ? new SolidColorBrush(Color.FromArgb(0xff, 0xff, 0x00, 0x00))  
                    : new SolidColorBrush(Color.FromArgb(102, 0, 0, 0));  
            }  
        }  
  
        public static ValidationResult ValidateSpecialCharacters(string zip)  
        {  
            var regexItem = new Regex("^[a-zA-Z0-9 ]*$");  
  
            return regexItem.IsMatch(zip)  
                ? ValidationResult.Success  
                : new ValidationResult("Cannot have Special characters");  
        }  

10444-1.gif

I dont want Property to be Constant

Universal Windows Platform (UWP)
0 comments No comments
{count} votes

Accepted answer
  1. gekka 8,061 Reputation points MVP
    2020-06-21T08:24:43.19+00:00

    Attribute arguments must be determined at compile time.

    Guessing from the code presented, it seems to me that you can achieve your goal by taking an instance from ValidationContext and switching it.

    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Collections.Generic;
    using System.Linq;
    
    public class ValidationTextBox : VMBase
    {
        public string Test { get; set; } = "ValidateSpecialCharacters";
    
        [CustomValidation(typeof(ValidationTextBox), nameof(CustomMethod))]
        public string GeneralDescription
        {
            get => _generalDescription;
            set
            {
                SetValue(ref _generalDescription, value);
            }
        }
        private string _generalDescription;
    
        public static System.ComponentModel.DataAnnotations.ValidationResult CustomMethod(string value, ValidationContext context)
        {
            var instance = (ValidationTextBox)context.ObjectInstance;
            switch (instance.Test)
            {
            case nameof(ValidationTextBox.ValidateSpecialCharacters):
                return ValidationTextBox.ValidateSpecialCharacters(value, context);
            default:
                return new System.ComponentModel.DataAnnotations.ValidationResult("Invalid Parameter");
            }
        }
        public static ValidationResult ValidateSpecialCharacters(string value, ValidationContext context)
        {
            var regexItem = new System.Text.RegularExpressions.Regex("^[a-zA-Z0-9 ]*$");
    
            return regexItem.IsMatch(value)
                ? ValidationResult.Success
                : new ValidationResult("Cannot have Special characters");
    
        }
    }
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful