Validation Rule Dependency

Validation Rule Dependency

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


ballistic posted on Thursday, April 28, 2011

I have a site where members are able to select the country they are from.  If they are from the US, I need to validate that they enter a zip code. If international, I need to validate that a region and city name are specified.

I have two questions with validating several dependent properties...

Question one:
Validation is triggered as each property is set, therefore I cannot completely guarantee that if a zip code is being set, the country has been previously selected.  Is this something that can be solved by ensuring that the zip code gets set before the zip code?

Question two:
If the  values are stored in the following properties, how should my dependencies be set up?
CountryIdProperty
ZipCodeProperty
RegionProperty 
CityProperty 

I'm migrating over from 3.8 and trying to figure out the new way of doing this.

Thank you,

 - Andy 

RockfordLhotka replied on Thursday, April 28, 2011

You can use an authorization rule to prevent ZipCode from being writable until CountryId has been set.

JonnyBee replied on Friday, April 29, 2011

Hi Andy,

Take a look at the Samples\Net\cs\BusinessRuleDemo solution.
This shows pretty much of what you need to do.

1. You need to create Authz rules to determine is user can write into fields (based on country)
2. Use a schortcut rule like StopIfNotCanWrite with priority -1 - available on CslaContrib (no validation rules to run if user is not allowed to edit field)
3. Use regular DataAnnotation rules or Csla rules for Required on fields.

You could alternatively create inherited rules like:
RequiredIfUS and RequiredIfNonUS that inherits from the Csla Required rule and looks at the value of Country field befor calling base.Execute(context);

But I like to possibility to use standard rules and the StopIfNotVanWrite to shortcut rules. This allows me to use DataAnnotation rules but only allow validation ro run when user is allowed to edt (write) into the field.

 

Copyright (c) Marimer LLC