Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Custom Data Annotation

$
0
0

I have the following, if I type in to a text box with a binding that has this annotation

       [ExcludeChar("@")] // Custom data annotation
            public string APPLICANT_Surname;

it steps through and hits the right line but not not show as an error, the other annotations do.

    public class ExcludeChar : ValidationAttribute
    {
        private readonly string _chars;

        public ExcludeChar(string chars)
            : base("{0} contains invalid characters")
        {
            _chars = chars;

        }

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (value != null)
            {
                for (int i = 0; i < _chars.Length; i++)
                {

                    var valueAsString = value.ToString();
                    if (valueAsString.Contains(_chars[i].ToString()))
                    {

                        var errorMessage = FormatErrorMessage(validationContext.DisplayName);
                        return new ValidationResult(errorMessage);
                    }

                }
            }
            return ValidationResult.Success;
        }
    }
Am I missing something?



Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>