JP White

JP White

  • NA
  • 7
  • 1.3k

Custom Attribute issue

Aug 9 2016 11:59 AM
Hello,
 

I have a CSLA object with two managed properties and a custom Attribute. The requirements is to have at least one property null.

In other words: If Property A is set to something and property B already has a value, then property A and B becomes invalid. Upon Blanking property B, property A should become valid and vise versa.

To solve this issue I called the Validator.ValidateProperty in the property setter to validate property A when B is set and vise versa.

The problem is that error provider is not updating. When Property A has a value and property gets updated, the error provider appears around the two boxes, which is perfectly fine. When blanking property A, the error provider goes away from txtBoxA and stays around txtBoxB even though I triggered the validation of property B after property A is set. Please note the second I try to modify Property B the error provider disappears. I look like the way I'm not invoking the validation correctly.

This issue is driving me insane. I'm not sure what I'm doing wrong.

 
Other ideas are appreciated !
 
 
  1. [AttributeUsage(AttributeTargets.Property | AttributeTargets.Class, AllowMultiple = true, Inherited = true)]  
  2.   
  3.   
  4.          class CustomAttribute : ValidationAttribute  
  5.         {  
  6.         private readonly string _other;  
  7.         public CustomAttribute(string other)  
  8.             {  
  9.             _other = other;  
  10.             }  
  11.   
  12.         protected override ValidationResult IsValid(object value, ValidationContext validationContext)  
  13.             {  
  14.             var property = validationContext.ObjectType.GetProperty(_other);  
  15.             if (property == null)  
  16.                 {  
  17.                 return new ValidationResult(  
  18.                     string.Format("Unknown property: {0}", _other)  
  19.                 );  
  20.                 }  
  21.             var otherValue = property.GetValue(validationContext.ObjectInstance, null);  
  22.   
  23.             if (!String.IsNullOrEmpty(value.ToString()) && !String.IsNullOrEmpty(otherValue.ToString()))  
  24.                 {  
  25.   
  26.                 return new ValidationResult("At least on property has to be null !");  
  27.                 }  
  28.             return null;  
  29.             }  
  30.         }  
  31.   
  32.   
  33.   
  34.   
  35.         public class Example : BusinessBase<Example>  
  36.         {  
  37.             public static PropertyInfo<string> AProperty = RegisterProperty<String>(p => p.A);  
  38.         [CustomAttribute("B")]  
  39.         public string A  
  40.             {  
  41.             get { return GetProperty(AProperty); }  
  42.             set { SetProperty(AProperty, value);  
  43.   
  44.              if (B != "")  
  45.                     {  
  46.                     try  
  47.                         {  
  48.                         Validator.ValidateProperty(B, new ValidationContext(this) { MemberName = "B" });  
  49.                         }  
  50.                     catch (Exception)  
  51.                         {  
  52.   
  53.   
  54.                         }  
  55.   
  56.                     }  
  57.                 }  
  58.   
  59.             }  
  60.         public static readonly PropertyInfo<string> BProperty = RegisterProperty<String>(p => p.B);  
  61.         [CustomAttribute("A")]  
  62.         public string B  
  63.             {  
  64.             get { return GetProperty(BProperty); }  
  65.             set { SetProperty(BProperty, value);  
  66.   
  67.                 if (A != "")  
  68.                     {  
  69.                     try  
  70.                         {  
  71.                         Validator.ValidateProperty(A, new ValidationContext(this) { MemberName = "A" });  
  72.                         }  
  73.                     catch (Exception)  
  74.                         {  
  75.   
  76.   
  77.                         }  
  78.   
  79.                     }  
  80.   
  81.                 }  
  82.             }  
  83.         }  
  
 
 

Answers (1)