I think you need to add the same rule to your second property.
Dependent property simply means that when rules for one of them are run, rules
for the other one are run as well, and BuiltBy property in your example has no
rules.
ValidationRules.AddRule<PartOrder>(
EnsureSupplierBuiltBy,
SupplierProperty
);
ValidationRules.AddRule<PartOrder>(
EnsureSupplierBuiltBy,
BuiltByProperty
);
ValidationRules.AddDependentProperty(
BuiltByProperty,
SupplierProperty,
true
);
Sergey Barskiy
Principal Consultant
office: 678.405.0687 |
mobile: 404.388.1899
Microsoft Worldwide Partner of the Year | Custom
Development Solutions, Technical Innovation
From: ajj3085
[mailto:cslanet@lhotka.net]
Sent: Friday, October 24, 2008 3:34 PM
To: Sergey Barskiy
Subject: [CSLA .NET] AddDependentProperty misunderstanding?
Hi,
I think maybe I'm misunderstanding... but if I have the following:
ValidationRules.AddRule<PartOrder>(
EnsureSupplierBuiltBy,
SupperProperty
);
ValidationRules.AddDependentProperty(
BuiltByProperty,
SupplierProperty,
true
);
private static
bool EnsureSupplierBuiltBy( PartOrder order,
RuleArgs e ) {
bool result;
string supplier, builtBy;
result = true;
supplier =
order.ReadProperty( SupplierProperty );
builtBy =
order.ReadProperty( BuiltByProperty );
if ( string.IsNullOrEmpty(
supplier ) && string.IsNullOrEmpty(
builtBy ) ) {
result = false;
e.Description = Properties.Resources.PartOrderSupplierOrBuiltByRequired;
}
if ( !( string.IsNullOrEmpty(
supplier ) || string.IsNullOrEmpty( builtBy ) )
) {
result = false;
e.Description
= Properties.Resources.PartOrderSupplierXorBuilt;
}
return result;
}
Now, shouldn't the rule be run when BuiltBy is changed? And if it is,
shouldn't there be a broken rule tied to the BuiltBy property as well as the
Supplier property?
Thanks
Andy
The rule runs, but if it's broken then the error (broken rule, error provider, etc.) get associated with original property referenced in AddRule, not the dependent property whose change caused it to fire.
All the dependent property association does is force the other rules to fire -- they are still associated with the original property.
I have taken advantage of this to associate an message with something user can see versus a hidden item that is actually broken.
In addition to the other comments above, you have a typo in the first thread:
ValidationRules.AddRule<PartOrder>(
EnsureSupplierBuiltBy,
SupperProperty <---- TYPO
);
ValidationRules.AddDependentProperty(
BuiltByProperty,
SupplierProperty,
true
);
Joe
Copyright (c) Marimer LLC