Tuesday, July 8, 2008

validators in DCToolBox

I copied into DCToolBox a bunch of the validators that Jinghua has written. The idea is that client side validation will be run first but in a client with Javascript disabled, these server side validators will run instead. I modified a few of them, which were for optional fields, to take an optional T/F parameter. Eventually all of the validators should have this, then they can be used for both required or optional fields.

I also added some new ones, including this:


public static bool AtLeastOne(List listOfResults, string msg, CustomValidator validator)
{
bool bOK = false;
foreach (bool i in listOfResults){
if (i == true)
{
bOK = true;
break;
}
}
//check only if no true items - meaning no field was answered
if (!bOK)
{
validator.ErrorMessage += String.Format(msg);
bOK = false;
}

return bOK;
}


This is for those situations where at least one of several fields must be answered - for example if you have 4 phone number fields, at least one of them must be filled in. Check all of them, returning a bool indicating if they are answered, and then collect those bools into a List<> and send that list to this function.

I have also written a few functions for returning a simple t/f to indicate whether a field has an answer to work with the above function.

Questions? Comments? Let 'er rip! If you need an example of how to use these validators in a project then let us know and one of us can post it.

1 comment:

JK said...

layla,

I think some examples would be much appreciated. If at all possible maybe some more of those famous video tutorials :)