Conditional Validation Rules?

Conditional Validation Rules?

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


jkellywilkerson posted on Tuesday, December 05, 2006

Hello all,

I have a use case that requires that supervisors have the ability to override the required fields during the entry of data - mainly because they do not have access to the data or it was not included on an application, etc.  Their intent is to require certain fields, such as, company name, address, city, phone, and others, but, if the application does not contain the phone number, the would like to be able to temporarily override the requirements until the information is obtained and continue the setup operation.

Has anyone dealt with this issue?  I have included an '_override' field as part of the BO, only available to those in particular roles, that 'turns off' the validationrules.addrule lines for the AddValidationRules() method, something like this:

if (!_override){ ValidationRules.AddRule(...StringRequired, "Phone"); etc. }

Will this even work?  Is the rule already loaded by the time the user selects the 'Override' checkbox?

Thanks in advance for any help,

Kelly.

ajj3085 replied on Tuesday, December 05, 2006

I think you'd have to use instance rules in this case, because override is an instance variable.  It would have to be, because as soon as the type rules are loaded, you don't have a way to clear them again if override is set back to false.

jkellywilkerson replied on Tuesday, December 05, 2006

Hey Andy,

Thanks for the reply.  I guess since I had several requirements that were affected by this, I tried to take a shortcut and cover them all at the same time, but, after looking at it in greater detail, and considering this is a rather small piece of the whole pie, it would be better to get it right the first time - even if it does take a little longer and requires a little more code.  So, for each conditional requirement I used something like this:

In the AddBusinessRules():

ValidationRules.AddRule(PhoneRequired, "Phone");

And just below:

private bool PhoneRequired(object target, Csla.Validation.RuleArgs e)

{

      if (!_override & (_phone.Length == 0))

      {

            e.Description = "Phone Number is required";

            return false;

      }

      else

            return true;

}

Does that look similar to your thinking?

Thanks again,

Kelly.

ajj3085 replied on Tuesday, December 05, 2006

Kelly,

If you're using type rules, you need to make the rule method static or you'll get a runtime error, assuming you're on whatever version of 2.x that added instance vs. type rules.

If you are on such a version, you'll need to make the method static and proabably the _override flag would come from the target object (unless the override is a system level setting, but it doesn't sound like that from your description).

Oh, you might want to have a SetOverride method, and that method would check that the user has permissions to actually override the rules, as I also assume some users shouldn't be allowed to leave the fields blank.

HTH
Andy

skagen00 replied on Tuesday, December 05, 2006

Maybe these are not required fields.

Maybe you have a property on your application that determines whether or not an application is "complete", which checks to see if everything is supplied in order to meet completeness.

Another option might be to use "Warning" business rules that allow saving but presents warning messages that "address is not supplied", etc.

The other option is what you're mentioning - though I'd almost go as far as making this a property "Override Application Required Fields" and potentially put write access security on that property so that only some users could skip required fields by selecting this. I would also make this a persistant property of the BO, so that someone else pulling up the record is able to manipulate it if they're doing something unrelated to the required fields that someone else didn't supply.

jkellywilkerson replied on Tuesday, December 05, 2006

Hey,

Thanks for the reply...They are actually required fields; however, due to the nature of the biz, on rare occasion, we receive an incomplete application and decide to force the app into the system to keep from holding up the process.  Of course, there are certain fields that are "More Required" than others and cannot be overridden.

I do like the idea of the "Warning Rules" and will look into that a little further.

The "Override" feature is definately role-based so that only supervisors and sys-admins can use it; you just can't have it any other way.

Thanks again,

Kelly.

xal replied on Tuesday, December 05, 2006

You can do that exact same thing with type rules. You only need to use the target to acces the values.


I would not do conditional adding of the rules because you might get into a mess, even worse, if certain users have access to changing the value for override, you're stuck with having (or not) the rules as a part of your object.

So I'd say, add all the rules as type rules, and check for override inside the rule, which is exactly what you did in your code sample, only the method should be static.


Copyright (c) Marimer LLC