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 !
- [AttributeUsage(AttributeTargets.Property | AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
-
-
- class CustomAttribute : ValidationAttribute
- {
- private readonly string _other;
- public CustomAttribute(string other)
- {
- _other = other;
- }
-
- protected override ValidationResult IsValid(object value, ValidationContext validationContext)
- {
- var property = validationContext.ObjectType.GetProperty(_other);
- if (property == null)
- {
- return new ValidationResult(
- string.Format("Unknown property: {0}", _other)
- );
- }
- var otherValue = property.GetValue(validationContext.ObjectInstance, null);
-
- if (!String.IsNullOrEmpty(value.ToString()) && !String.IsNullOrEmpty(otherValue.ToString()))
- {
-
- return new ValidationResult("At least on property has to be null !");
- }
- return null;
- }
- }
-
-
-
-
- public class Example : BusinessBase<Example>
- {
- public static PropertyInfo<string> AProperty = RegisterProperty<String>(p => p.A);
- [CustomAttribute("B")]
- public string A
- {
- get { return GetProperty(AProperty); }
- set { SetProperty(AProperty, value);
-
- if (B != "")
- {
- try
- {
- Validator.ValidateProperty(B, new ValidationContext(this) { MemberName = "B" });
- }
- catch (Exception)
- {
-
-
- }
-
- }
- }
-
- }
- public static readonly PropertyInfo<string> BProperty = RegisterProperty<String>(p => p.B);
- [CustomAttribute("A")]
- public string B
- {
- get { return GetProperty(BProperty); }
- set { SetProperty(BProperty, value);
-
- if (A != "")
- {
- try
- {
- Validator.ValidateProperty(A, new ValidationContext(this) { MemberName = "A" });
- }
- catch (Exception)
- {
-
-
- }
-
- }
-
- }
- }
- }