3.5 Validation Confusion
Old forum URL: forums.lhotka.net/forums/t/5483.aspx
bcrowell posted on Sunday, September 28, 2008
I have a basic business object with a few basic rules like string required, etc. I have it bound to a form via a BindingSource. When I instantiate a new object, I am able to save it right away without filling in the required fields. It seems the validation isn't checked when the IsNew= true.
However, when I open the same object, I'm not able to update it without filling in the required fields first.
I'm having this problem across my app with newly instantiated objects. I can't apply rules until the object has been saved once. Any ideas would be very helpful.
Thanks much.
Byron
RockfordLhotka replied on Sunday, September 28, 2008
You need to call ValidationRules.CheckRules() in your DataPortal_Create() method.bcrowell replied on Tuesday, September 30, 2008
Hi Rocky,
So I was getting errors all over the place when I newed up on object that called the validation in the create method. That's actually why I ended up removing it to get the system to work. Here's what I have:
<RunLocal()> _
Protected Overrides Sub DataPortal_Create()
LoadProperty(Of Integer)(CONTAINERIDProperty, 0)
ValidationRules.CheckRules()
End Sub
And here's my validation section:
#Region " Validation Rules "
Protected Overrides Sub AddBusinessRules()
ValidationRules.AddRule(AddressOf Validation.CommonRules.StringRequired, New Csla.Validation.RuleArgs(DescriptionProperty))
ValidationRules.AddRule(AddressOf Validation.CommonRules.StringRequired, New Csla.Validation.RuleArgs(DescriptionProperty).Description = "Must Select a Display Page or an Email Template")
ValidationRules.AddRule(AddressOf Validation.CommonRules.IntegerMinValue, New Validation.CommonRules.IntegerMinValueRuleArgs(HowManyProperty, 1))
End Sub
#End Region
When I call this:
Dim _CC As ContentContainer
_CC = _CC.NewContainer
I get this error:
CSLA.Validation.ValidationEXception: Validation rule rule://StringRequired/False failed in
property False --> System.MissingMemberException: Public member 'False' on type 'ContentContainer' not found.
What am I missing. I'm sure it's something really stupid. Thanks.
RockfordLhotka replied on Tuesday, September 30, 2008
The first thing is that you should never new up an object. Your
objects should have non-public constructors to prevent the use of the new
keyword. CSLA doesn’t support the use of the new keyword – objects are
created through the data portal, and the data portal call is normally contained
in a factory method. No other approach is supported.
Also, notice that you are adding the StringRequired rule to a
property twice. I suspect that’d cause a problem – it certainly isn’t
a supported concept. The one workable variation here, is if you have different
custom RuleArgs subclasses then you can add the same rule twice, because
the rule:// URI would resolve to different rule names. But I’m not even
sure that I’d recommend doing such a thing – that seems way overly
complicated on so many levels…
Rocky
JoeFallon1 replied on Wednesday, October 01, 2008
What is this:
ValidationRules.AddRule(AddressOf Validation.CommonRules.StringRequired, New Csla.Validation.RuleArgs(DescriptionProperty).Description = "Must Select a Display Page or an Email Template")
It looks like you are trying to set the Description property of the RuleArgs class while setting up rules. That is a No No.
Only set e.Description when the rule fails!
Remove this line of code and see if things start working.
Joe
bcrowell replied on Saturday, October 04, 2008
Hi Rocky,
Yes, when I said "new up" I meant using the dataportal. I'm proud to say that I do at least understand that much about CSLA after a year of working with it.
It looks like i need to spend more time understanding the Validation concepts so I'll dig into that part of the book again.
In reviewing the ProjectTracker application, I see that you have a string required and string length rule on the Resource.vb object but then you don't have a Create method at all and thus don't call the ValidationRules.Checkrules command. Can you explain why that is.
Many, many thanks.
Byron
RockfordLhotka replied on Monday, October 06, 2008
The base class (BusinessBase) has a default implementation of
DataPortal_Create() that does nothing but call ValidationRules.CheckRules()
(and it is marked with RunLocal).
This was introduced in version 3.5 to reduce the size of your
code, because a LOT of objects don’t do any meaningful initialization.
Rocky
Copyright (c) Marimer LLC