Dependant Rule validation

Dependant Rule validation

Old forum URL: forums.lhotka.net/forums/t/1815.aspx


razorkai posted on Wednesday, November 22, 2006

I have two properties, AccountTypeId and NavAccountNo.  There are also two instance rules, one which validates that the NavAccountNo exists and the second which validates that the AccountTypeId selected is valid for the entered NavAccountNo.

I only want the AccountTypeId validation done if the NavAccountNo is validated as existing, but I also need to make sure that if they change the NavAccountNo to a valid entry the AccountTypeId validation will get done.  So I tried doing this

ValidationRules.AddInstanceRule(NavisionAccountExists, "NavAccountNo", 1);

ValidationRules.AddInstanceRule(CreditCardExists, "AccountTypeId", 2);

ValidationRules.AddDependantProperty("NavAccountNo", "AccountTypeId");

This works nearly perfectly. The only issue is that this displays an error provider error symbol against the AccountTypeId bound control when the NavAccountNo does not exist.  The tooltip for the error is the one for the NavisionAccountExists rule.  So I end up with the same error displayed against two properties.  How can I get the two properties to trigger each others validation rules but only show the error icon against the relevant property?  Hope this makes sense!

RockfordLhotka replied on Wednesday, November 22, 2006

So your AccountTypeId is getting the same tooltip as NavAccountNo? That doesn't make any sense - the NavisionAccountExists rule shouldn't be running for the AccountTypeId property...

Or are you saying that you just don't want CreditCardExists to run if NavisionAccountExists fails?

razorkai replied on Thursday, November 23, 2006

I do not want CreditCardExists to run if NavisionAccountExits fails. I thought that the priority parameter I used when adding the rules would facilitate this? I don 't understand the tooltip thing either, but it only happens if I have the AddDependantProperty line. If I comment this out then the tooltip will only display next to the NavAccountNo property, which is correct - but then I don't get the CreditCardExists rule to fire if someone changes the NavAccountNo property and it passes the NavisionAccountExits validation. Hope this is clearer

RockfordLhotka replied on Thursday, November 23, 2006

The priority setting is used to prioritize the order in which rules are applied for each specific property. It does nothing across properties.
 
What you need is a rule that runs, and fails, on the same property as CreditCardExists, and _that_ rule should be at priority 0 so it runs before CreditCardExists.
 
But that's not really what you want either. Hmm...
 
I'll have to give this some thought as I digest my turkey dinner this afternoon.
 
Rocky
 


From: razorkai [mailto:cslanet@lhotka.net]
Sent: Thursday, November 23, 2006 2:25 AM
To: rocky@lhotka.net
Subject: Re: [CSLA .NET] Dependant Rule validation

I do not want CreditCardExists to run if NavisionAccountExits fails. I thought that the priority parameter I used when adding the rules would facilitate this? I don 't understand the tooltip thing either, but it only happens if I have the AddDependantProperty line. If I comment this out then the tooltip will only display next to the NavAccountNo property, which is correct - but then I don't get the CreditCardExists rule to fire if someone changes the NavAccountNo property and it passes the NavisionAccountExits validation. Hope this is clearer




razorkai replied on Thursday, November 23, 2006

Further information:  Don't worry about the tooltip issue, as that was the guy who wrote the validation stored procedures I am using getting it wrong.   Thanks for the info about the rule priorities, they make more sense now.  Look forward to seeing what can be done regarding my rule requirement!

RockfordLhotka replied on Monday, November 27, 2006

OK, thanks for letting me know about the tooltip issue - I couldn't figure that one out...

But I think I have a workable answer for the short-circuiting issue. Before implementing it I want to get feedback on the idea though.

One of the things you can do to short-circuit processing for a property is have a rule method explicitly set e.StopProcessing to True, and then return False. Regardless of all other factors, this combination will cause subsequent rules for the property to be ignored.

If I change this slightly I think I can provide an answer to your scenario.

If e.StopProcessing = True causes short-circuiting in all cases (even if the rule returns True), then you could create a required-field rule method that doesn't fail, but does stop processing of subsequent rules.

Or, to be slightly more clever, you could create a rule that doesn't fail, but stops processing of subsequent rules if some other property has 1+ broken rules. This could be a new common rule, because if my scheme works I could see this being quite useful in many cases.

So what you'd do is:

  1. Run normal processing on PropertyA - causing it to have a broken rule if it is missing
  2. Use this new PropertyHasNoErrors rule on PropertyB, at priority 0
  3. Use any other rules on PropertyB at priority 1 or higher

Since PropertyHasNoErrors would not "fail", it wouldn't trigger a tooltip. However, with my proposed e.StopProcessing mod, it would stop all subsequent rules from processing for PropertyB, which gets you the desired result.

What do you think?

 

razorkai replied on Tuesday, November 28, 2006

Hi Rocky.

That sounds like a good solution.  My only concern is that by changing StopProcessing to short circuit in all cases you could break other peoples code that relies on the way it behaves now.  This concern may prove to be unfounded, maybe other people in the forum have some feedback?

John.

RockfordLhotka replied on Tuesday, November 28, 2006

I agree with the concern about breaking existing code. And yet I am becoming increasingly convinced that this is how I should have done this in the first place Sad [:(]

Copyright (c) Marimer LLC