Validation Rules
Old forum URL: forums.lhotka.net/forums/t/437.aspx
Crestline posted on Wednesday, June 21, 2006
We are getting our heads wrapped around Validation rules at the moment and have a couple questions.
As we understand it, a validation rule is checked when
PropertyHasChanged() is called or when it is explicitly called.
How would you implement a validation rule for a 'required property' if
the user never enters anything for that property. Because
PropertyHasChanged() will never fire, where should we explicitly call
CheckRules() to make sure it is in fact been checked?
What is a good way to handle saves when an object is invalid?
Right now just an exception is thrown because the save is allowed to
run. How do we go about intercepting before the save and crash
happens?
Thanks for any ideas,
BR
ajj3085 replied on Wednesday, June 21, 2006
There's a couple of things you can do. For one, in your DataPortal_Create, you could set the value via the property (which would call the propertyhaschanged, which in turn will run rules). Or you could set the intial value by setting the fields directly, and before your DataPortal_Create exists, call ValidationRules.CheckRules, to run all teh checks at once.
Regarding saves in an invalid state.. that may mean you have another BO requirement. For example, you may have a Quote which you can save at any time (and thus the rules only reflect the data required to save a quote, they don't tell you if the quote is 'valid'). You could then have an order object which is created from a quote... so: Order myOrder = Order.NewOrder( myQuote ); Which would bomb if the quote was not in a 'valid enough' state to create the order.
There has been much discussion on this topic in the past; you might want to search the forum for other ideas as well.
HTH
Andy
Crestline replied on Wednesday, June 21, 2006
Also to double check, when data binding our objects, is the set method
of the public property actually run or does datatbinding just effect
the internal properties? If it's just internal properties, then
the PropertyHasChanged() will never fire...
ajj3085 replied on Wednesday, June 21, 2006
Crestline: Also to double check, when data binding our objects, is the set method
of the public property actually run or does datatbinding just effect
the internal properties? If it's just internal properties, then
the PropertyHasChanged() will never fire...
Data binding only works with public properties and fields, so the PropertyhasChanged will be called properly.
Crestline replied on Wednesday, June 21, 2006
Ok, great. Thanks for the clarification. We should be able to get this figured out.
Copyright (c) Marimer LLC