Validation Rules
Old forum URL: forums.lhotka.net/forums/t/933.aspx
vargasbo posted on Thursday, August 17, 2006
I'm trying to do the following, get a value and check to see if it's the correct type, and if required is present. The issue i'm having is that the required and data type aren't being passed in since they are an instance reference. Any ideas how to pass those values?
ValidationRules.AddRule(new RuleHandler(Validator.CheckCustomServiceField), new Validator.CustomServiceRuleArgs("CustomFieldValueType",this.CustomFieldRequired,this.CustomFieldDataType));
Which ties into...
public static bool CheckCustomServiceField(object target, RuleArgs e)
{
string valueType = (string)Utilities.CallByName(target, e.PropertyName, CallType.Get);
bool required = ((CustomServiceRuleArgs)e).Required;
ValidationDataType dataType = ((CustomServiceRuleArgs)e).DataType;
if (required && String.IsNullOrEmpty(valueType))
{
e.Description = "Missing required field";
return false;
}
switch (dataType)
{
case ValidationDataType.String:
return true;
break;
case ValidationDataType.Integer:
int p;
return Int32.TryParse(valueType, out p);
break;
case ValidationDataType.Double:
double result;
return double.TryParse(valueType, out result);
break;
case ValidationDataType.Date:
DateTime mresult;
return DateTime.TryParse(valueType, out mresult);
break;
case ValidationDataType.Currency:
double lresult;
return double.TryParse(valueType, out lresult);
break;
default:
return false;
break;
}
return true;
}
Copyright (c) Marimer LLC