Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6839805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:45:49+00:00 2026-05-26T23:45:49+00:00

I have a ValidationAttribute that I have created which is shared between the Server,

  • 0

I have a ValidationAttribute that I have created which is shared between the Server, and Client. In order to get the validation attribute to properly generate to the client when referenced within a data helper class I had to be very specific in how I built it.

The problem I’m having is that for some reason when I return a ValidationResult from my custom validation attribute class it is not handled the same as other validation attributes on the client UI. Instead of displaying the error it does nothing. It will properly validate the object though, it just doesn’t display the failed validation result.

Below is the code for one of my custom validation classes.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;

namespace Project.Web.DataLayer.ValidationAttributes
{
    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
    public class DisallowedChars : ValidationAttribute
    {
        public string DisallowedCharacters
        {
            get
            {
                return new string(this.disallowedCharacters);
            }

            set
            {
                this.disallowedCharacters = (!this.CaseSensitive ?     value.ToLower().ToCharArray() : value.ToCharArray());
            }
        }

        private char[] disallowedCharacters = null;

        private bool caseSensitive;

        public bool CaseSensitive
        {
            get
            {
                return this.caseSensitive;
            }

            set
            {
                this.caseSensitive = value;
            }
        }

        protected override ValidationResult IsValid(object value, ValidationContext    validationContext)
        {
            if (value != null && this.disallowedCharacters.Count() > 0)
            {
                string Value = value.ToString();

                foreach(char val in this.disallowedCharacters)
                {
                    if ((!this.CaseSensitive && Value.ToLower().Contains(val)) ||     Value.Contains(val))
                    {
                        return new ValidationResult(string.Format(this.ErrorMessage != null ? this.ErrorMessage : "'{0}' is not allowed an allowed character.", val.ToString()));
                    }
                }
            }

            return ValidationResult.Success;
        }
    }
}

This is how I use it above my Properties on both the server, and client.

[DisallowedChars(DisallowedCharacters = "=")]

And I’ve tried several different ways of setting up the binding.

{Binding Value, NotifyOnValidationError=True}

As well as

{Binding Value, NotifyOnValidationError=True, ValidatesOnDataErrors=True, ValidatesOnExceptions=True, ValidatesOnNotifyDataErrors=True}

None of these seem to make the forms that they are bound too validate the entries. I’ve tried using this attribute on values that are bound to TextBoxes, XamGrids, and neither of those properly validate like they should.

This problem only seems to be when I am attempting to use the ValidationResult on the server side. If I use the validation result on a value in my view model then it will properly validate. I need to find a way to make this properly validate from the generated code though.

Any thoughts would be very much appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:45:50+00:00Added an answer on May 26, 2026 at 11:45 pm

    You need to specify the MemberNames that are associated with the ValidationResult. The constructor of ValidationResult has an additional parameter to specify the properties that are associated with the result. If you do not specify any properties, the result is handled as a validation error on entity level.

    So in your case, it should be fixed, when you pass in the name of the property to the constructor of the ValidationResult.

    protected override ValidationResult IsValid(object value, ValidationContext validationContext) {
     if (value != null && this.disallowedCharacters.Count() > 0) {
       string Value = value.ToString();
    
       foreach(char val in this.disallowedCharacters) {
         if ((!this.CaseSensitive && Value.ToLower().Contains(val)) || Value.Contains(val)) {
           //return new ValidationResult(string.Format(this.ErrorMessage != null ? this.ErrorMessage : "'{0}' is not allowed an allowed character.", val.ToString()));
           string errorMessage = string.Format(this.ErrorMessage != null ? this.ErrorMessage : "'{0}' is not allowed an allowed character.", val.ToString());
           return new ValidationResult(errorMessage, new string[] { validationContext.MemberName});
         }
       }
     }
    
     return ValidationResult.Success;
    }
    

    For the bindings you don´t need to specify anything else. So the simple Binding

    {Binding Value}
    

    should display errors, cause ValidatesOnNotifyDataErrors is set to true implicitly. NotifyOnValidationError populates ValidationErrors to other elements like ValidationSummary.

    Jeff Handly has a really goog blog post about Validation in WCF Ria Services and Silverlight, i can recommened to read.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a custom validation attribute by subclassing ValidationAttribute. The attribute is applied
I have a validation attribute that inherits from ValidationAttribute. However, the particular usage of
I have written a custom credit card validation attribute that checks CardNumber property is
I have created a Custom Validation Attribute: public sealed class DateAttribute : DataTypeAttribute {
I have created custom validation attribute [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited =
I have created a custom validator deriving from ValidationAttribute. My undertsandng is that it
I have a custom validation attribute derived from action filter attribute. Currently the attribute
I have a DataAnnotationValidator that I created. I am currently trying to test it
I have created the following custom attribute to assist me with validating a required
I have an ASP.NET MVC 2 project in which I've created a data transfer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.