Validation trouble

Validation trouble

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


CrikeyMoses posted on Monday, November 07, 2011

I am running Silverlight, CSLA 4.1, MVVM.

I have a simple class called "Company" which has a few properties and i implement the BusinessRules as follows:  BusinessRules.AddRule(new Csla.Rules.CommonRules.Required(ContactPersonProperty));

My problem is that when i do a CREATE the validation on the "Contact Person" text box works fine, but when i do a FETCH I have to do a BusinessRules.CheckRules(); for any validation to fire. In addition to that once the validation has fired and i type something into the "Contact Person" text box and tab away, the little red border and error info stays there even tho i can see the correct value being passed and set in the SET method of "ContactPersonProperty".

Can anyone please help me on this issue, or explain why this happens?

JonnyBee replied on Monday, November 07, 2011

1. When you call fetch it is up to you (the developer) to decide if you consider the data in your database to be valid by default or to recheck business rules.

Also - when loading many rows of data it may cost a large amount of time to execute the validation rules.

2. Make sure that your property set method uses SetProperty. It is only SetProperty that will retrigger business rules and raise property changed to the ui. If your code uses LoadProperty then no business rules is run an no ProeprtyChanged is raised to notify ui.

If it still fails then make sure to upgrade to the latest version of CslaPropertyStatus from 4.2.

CrikeyMoses replied on Monday, November 07, 2011

Thanks guys, that worked a treat! And i now understand why i need to call the CheckRules, the other issue was that i was using LoadProperty and not SetProperty.

Thanks again.

StefanCop replied on Monday, November 07, 2011

If you have a base.DataPortal_Create() in your overriden method, the base method will invoke a BusinessRules.CheckRules().

The DataPortal_Fetch on the other hand does not a CheckRules(), because it is assumed that values from database are correct. If this in your case is not true (ie. a web service) then your DataProtal_Fetch should invoke BusinessRules.CheckRules() to have again this initial vaildation.

The fact, that the validation error (red border) doesn't disappear indicates another problem. How do you set the CSLA property? Do you use SetProperty(), which raises the property changed event and thus runs business rules; or LoadProperty, which doesn't?

Copyright (c) Marimer LLC