Rank: Administration
Groups: Registered, Administrators Joined: 8/17/2010(UTC) Posts: 6 Location: Denver
|
Most of the time when you create an InfoPath template, validation is done using rules. There are times, however, when you need to validate in code. Here is some sample code for performing a validation on a field. In this example, we are checking to see if the Month that the user selected is within the planning date range set by some assumptions. These assumptions are held in a cube called Planning Assumptions which we have queried when the form was first loaded. The values to validate against are held in a secondary data source. Code:
public void FiscalMonth_OnValidate(DataDOMEvent e)
{
// Only run the validation on an insert or an update, do not
// run the validation on a delete operation.
if (e.Operation == "Insert" || e.Operation == "Updated")
{
// See if a value has been specified yet. If not, ignore it
if (e.NewValue == null || string.IsNullOrEmpty(e.NewValue.ToString()))
return;
// Get the start and end date from the Planning Assumptions query
string endDate = es.GetDataSourceFieldValue("Planning Assumptions", "/dfs:myFields/dfs:dataFields/tns:PlanningAssumptionsResponse/Results/RowSet/Rows/EndPeriodValue");
string startDate = es.GetDataSourceFieldValue("Planning Assumptions", "/dfs:myFields/dfs:dataFields/tns:PlanningAssumptionsResponse/Results/RowSet/Rows/StartPeriodValue");
if (string.IsNullOrEmpty(endDate))
return;
// Get the current year selected by the user
string selYear = es.GetFieldValue("/my:myFields/my:secSelections/my:SelectedYear");
// Get the current month entered by the user and make sure it is two digits in length
string selMonth = e.NewValue.ToString();
if (selMonth.Length == 1)
selMonth = "0" + selMonth;
// Format the year and month into a comparable string
string selPeriod = string.Concat(selYear, selMonth);
// See if it is less than the start date, if so, then report an error.
if (string.Compare(selPeriod, startDate) < 0)
{
e.ReportError(e.Site, "Invalid date specified", false,
"You cannot add a CapEx item with an in service date [" + selPeriod +
"]\nbefore the current planning period [" + startDate + "].", 10, "modal");
}
// See if it is greater than the end date, if so, then report an error.
if (string.Compare(selPeriod, endDate) > 0)
{
e.ReportError(e.Site, "Invalid date specified", false,
"You cannot add a CapEx item with an in service date [" + selPeriod +
"]\nbeyond the current planning period [" + endDate + "].", 10, "modal");
}
}
}
|