IsValid and this.ValidationRules.CheckRules()

IsValid and this.ValidationRules.CheckRules()

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


triplea posted on Friday, September 14, 2007

I have an object which is definately not valid (e.g. a property has exceeded its max length). If I inspect the IsValid property, I get back true. If I call ApplyEdit() I still get back true. Now if I call the CheckRules() method for that object, IsValid is false.
I have been good and have a custom BusinessBase class in which I will simply override IsValid and call CheckRules() prior to base.IsValid but it seems somehow weird that this is not the current behaviour...
So could someone please point out if I am missing something out or am doing something wrong? Also, will severity levels mix anything up (when I start using them that is...)?

FYI I am using CSLA 2.1.4

skagen00 replied on Friday, September 14, 2007

It sounds like you are not having your business rule triggered when your string changes and becomes invalid. Ensure that your PropertyHasChanged() method is getting fired in your string property's setter and ensure that your business rule is hooked up to the property correctly. The moment your string changes in the business object you should be triggering your validation rule... You'd be getting IsValid to flip to false when you do CheckRules because you're essentially firing all of your business rules off.

That's what I'd suggest based on what you wrote above... throw a breakpoint in your validation rule, and hop into your UI and change your control tied to the string property - if your validation rule doesn't fire, then there's a problem.

Chris

 

triplea replied on Friday, September 14, 2007

Thanks for that. Actually based on your reply let me be more specific (sorry am a bit tired...). The problem is I have a Customer editable root object containing an Orders editable list object. Its one of the Orders that is not valid.

For that Orders object I am calling the PropertyHasChanged in the property in question and in the Customer.IsValid override I do check on both base.IsValid and Orders.IsValid.

Is it possible that when adding an Order in the Customer.OrderList that is not valid (not even sure that's possible) that Customer won't reflect that in its own IsValid?

RockfordLhotka replied on Friday, September 14, 2007

Your root object does need to override IsValid and IsDirty to include the child list's state. This is described in Chapter 7 or 8. If you don't do the overrides, the child list's state won't impact the root object's properties and you'll have problems.

triplea replied on Friday, September 14, 2007

triplea:

For that Orders object I am calling the PropertyHasChanged in the property in question and in the Customer.IsValid override I do check on both base.IsValid and Orders.IsValid.

Thanks Rocky but I was doing that already... Smile [:)]

dean replied on Friday, September 14, 2007

Triplea,

Just to cover all the bases : Have you checked the actual order object that is not supposed to be valid to make sure it is not valid? If it is not valid then somewhere in your isvalid overrides there is something wrong.

You could put a breakpoint in the rule that doesn't seem to be firing and test that it is actually firing. Once it fires, check thru your object hierarchy and see if orders is not valid and then customer is not valid.

You could create a unit test to do this also.

Dean

jhw replied on Friday, September 14, 2007

Are you saying it should be invalid when it is new, but it showing valid. Or is it showing valid after the property has been set during use?

For new objects, you would of course call validationRules.checkrules in dataportal_create.

In the property you would call PropertyHasChanged.

 

I don't know that calling apply edit should have any effect on the valid state, though cancel edit would

 

Heath

triplea replied on Friday, September 14, 2007

Heath

The object is not new and thanks for reminding me that I should call CheckRules() in the dataportal_create method (was not doing it but added it and the object is valid at creation). I am convinced I somewhere missed something but still think that it would make sense if CheckRules was fired automatically from within IsValid...

jhw replied on Friday, September 14, 2007

I would have to agree with Chris that the problem must be in making sure that you PropertyHasChanged is being called and that the correct authorization rule is giving you the valid/invalid result you expect.

As for whether the child list is not reflecting the state of all its children, that is not a problem. If any children are invalid then the list WILL be invalid.

 

Heath

Copyright (c) Marimer LLC