Force validation on property even if value has not changed...

Force validation on property even if value has not changed...

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


ballistic posted on Tuesday, May 04, 2010

I have a CountryId property that when set, I need to always validate.

What is happening is that members register and do not need to set a countryId (using my class AccountRegistration).

Once they go to update an account, they are forced to select a country. However, since they initially signed up and did not set a countryId, I recorded NULL in the database, now, when I go to read this value, it gets converted to 0.  When the members update the new form without selecting a country, I am presented with 0.  So since the country value is 0 when it's read and 0 when it is updated, validation never kicks in.  

Is there a way to force SetProperty to trigger validation even if the value has not changed?

I do not want to use ValidationRules.CheckRules() to  check all the rules or ValidationRules.CheckRules(CountryIdProperty) to check the one property.  I would like to do it in the setter of the CountryId. 

 - Andres

tmg4340 replied on Tuesday, May 04, 2010

For what it's worth, there is an alternative way to look at this.

CSLA provides diffferent tools to allow you to not have to consider NULL values from the database.  This is done because, for the most part, a NULL value is not significant to the user interface.  However, in your case, it appears that it is.

Depending on how complex you want to make your code (and what your UI controls can manage), I see two different options:

1. Change your CountryId field to a nullable type.  CSLA supports these just fine, and if your UI controls can deal with it, the solution is relatively painless.

2. Change your NULL-to-0 conversion to something else - say, NULL-to- -1.

Either solution should give you what you want, which is a way to differentiate "no country selected in the UI" from "no country provided from the database".  That should get CSLA's validation subsystem to kick in without you having to do anything special.

HTH

- Scott

rsbaker0 replied on Wednesday, May 05, 2010

ballistic

I do not want to use ValidationRules.CheckRules() to  check all the rules or ValidationRules.CheckRules(CountryIdProperty) to check the one property.  I would like to do it in the setter of the CountryId. 

You could do something like this in your setter: (a slight hack but it might solve your particular problem here):

 set
{
   if (value != CountryId)
      SetProperty(CountryIdProperty, value)
   else if (value == 0)
      PropertyHasChanged(CountryIdProperty.Name);
}

Note, however the setter may not even be called if the user doesn't interact with the corresponding field on your form (not sure what your target UI platform is, but in WinForms controls are often smart enough not to call set property if you don't change anything), so a better solution is to use nullable types as suggested above.

Copyright (c) Marimer LLC