Skip to content

Custom Attribute issue

JP White

Asked Aug 9, 2016, 7:59 AM EDT1 answer
Share
LinkedInX
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.         }  

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0

    I solved the issue by creating a custom CSLA BusinessRule. I made the two properties dependent on each other.

     
    Added this method the Business Object Class Example#
    1. using Csla;  
    2. using Csla.Rules;  
    3. using System;  
    4. using System.ComponentModel.DataAnnotations;  
    5.   
    6. public class Example : BusinessBase<Example>    
    7.         {    
    8.             public static PropertyInfo<string> AProperty = RegisterProperty<String>(p => p.A);    
    9.         [CustomAttribute("B")]    
    10.         public string A    
    11.             {    
    12.             get { return GetProperty(AProperty); }    
    13.             set { SetProperty(AProperty, value); }    
    14.     
    15.             }    
    16.         public static readonly PropertyInfo<string> BProperty = RegisterProperty<String>(p => p.B);    
    17.         [CustomAttribute("A")]    
    18.         public string B    
    19.             {    
    20.             get { return GetProperty(BProperty); }    
    21.             set { SetProperty(BProperty, value); }    
    22.             }    
    23.          
    24.   
    25. protected override void AddBusinessRules()  
    26. {  
    27. BusinessRules.AddRule(new CustomBusinessRule(AProperty));  
    28. BusinessRules.AddRule(new CustomBusinessRule(BProperty));  
    29. BusinessRules.AddRule(new Csla.Rules.CommonRules.Dependency(AProperty, BProperty));  
    30. BusinessRules.AddRule(new Csla.Rules.CommonRules.Dependency(BProperty, AProperty));  
    31. }  
    32.   
    33. }   
     
        
     
    Created the Custom Rule
    1. using System;  
    2. using System.Collections.Generic;  
    3. using Csla.Rules;  
    4.   
    5. public class CustomBusinessRule : BusinessRule  
    6.         {  
    7.         public CustomBusinessRule (Csla.Core.IPropertyInfo primaryProperty) : base(primaryProperty)  
    8.             {  
    9.             InputProperties = new List<Csla.Core.IPropertyInfo> { primaryProperty };  
    10.             }  
    11.         protected override void Execute(RuleContext context)  
    12.             {  
    13.             Example target = (Example)context.Target;  
    14.             if (!string.IsNullOrEmpty(ReadProperty(target, Example.A).ToString()) && !String.IsNullOrEmpty(ReadProperty(target, Example.B).ToString()))  
    15.                 {  
    16.                 context.AddErrorResult("At least one property has to be Null !");  
    17.   
    18.                 }  
    19.             }  
    20.         }  
     
    0