BrokenRules Problem

BrokenRules Problem

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


mking posted on Thursday, August 03, 2006

I have a problem where the validation rules are not being inforced when inserting a new record.  They are being enforced properly when I update an existing record.

I'm using the CslaDataSource, a DetailsView control, and the Csla.Data.DataMapper object.  My business object is an EditableRoot (within an EditableRootList).  I'm using CodeSmith and the templates someone on this forum (thanks!) created.

When I put a breakpoint in in the IsValid function within BusinessBase.cs, I see that the ValidationRules collection is present with the right number of rules, and I see all the property values in my object as empty strings (violations of my rules) but the BrokenRulesCollection is empty and IsValid is true.  What do I need to do to force it to re-check the rules?

Another question I have is: can I get the Validation rules to contain error messages that are more meaningful to the user?  I'm using a function that was in the sample code I downloaded.  It loops through the broken rules and populates the red ErrorLabel.  I'd like to be able to specify a better message when I create the rule.  Instead of "* VenID: VenID can not be less than 1".  I'd like it to say "Please choose a Venue".  (Maybe the wording of error messages isn't meant to be in the BO layer?)

Thanks in advance for any help with this.  -Mike

xal replied on Thursday, August 03, 2006

For your first problem, just call ValidationRules.CheckRules() inside DataPortal_Create().
For your second problem, just write your own rule that handles that...

Public Function ValidateID(Byval target as Object, ByVal e as RuleArgs) As Boolean
If VenID = 0 Then
e.Description = "Please choose a value"
Return False
End If
Return True
End Function

Andrés

mking replied on Friday, August 04, 2006

Thanks Andrés.  You pointed me in the right direction and saved me a lot of time.

I caused my first problem by changing a line of code in NewEvent() (my business object is called Event) from

return DataPortal.Create<Event>();

to

return new Event();

I made this change because I was getting the error "Invalid Operation - create not allowed" and I wasn't really understanding what that error meant.  I see now that my CodeSmith generated code was not passing the Criteria object to DataPortal_Create, which was expecting it.  Now, the line of code looks like this (and it seems to work)

return DataPortal.Create<Event>(new Criteria(0, 0));

------------------------

For my second problem, I'm assuming I can add the ValidateID rule you posted to the ValidationsRules collection in the BO?

xal replied on Friday, August 04, 2006

Yes, that's the standard way for creating rules...
You'd then have to override the AddBusinessRules() method and call:

ValidationRules.AddRule(AddressOf methodName, "PropertyName")


Andrés

mking replied on Friday, August 04, 2006

Got it working.  Thanks for explaining that to me.

-Mike

Copyright (c) Marimer LLC