Note that there are some explanatory texts on larger screens.

plurals
  1. POValidationErrors of custom ValidationAttribute not properly displayed
    text
    copied!<p>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.</p> <p>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.</p> <p>Below is the code for one of my custom validation classes. </p> <pre><code>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 &amp;&amp; this.disallowedCharacters.Count() &gt; 0) { string Value = value.ToString(); foreach(char val in this.disallowedCharacters) { if ((!this.CaseSensitive &amp;&amp; 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; } } } </code></pre> <p>This is how I use it above my Properties on both the server, and client.</p> <pre><code>[DisallowedChars(DisallowedCharacters = "=")] </code></pre> <p>And I've tried several different ways of setting up the binding. </p> <pre><code>{Binding Value, NotifyOnValidationError=True} </code></pre> <p>As well as </p> <pre><code>{Binding Value, NotifyOnValidationError=True, ValidatesOnDataErrors=True, ValidatesOnExceptions=True, ValidatesOnNotifyDataErrors=True} </code></pre> <p>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. </p> <p>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.</p> <p>Any thoughts would be very much appreciated.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload