.Net API Validate method for Screen level validations

By | May 15, 2013

First let us see what happens when the Validate method is not used for the .NET API screen validation.
Below is the new entry screen designed with API and with no validate method implemented.

On clicking Save button leaving all required fields blank, validations must be fired to warn user to enter required values in appropriate fields as follows which will not happen.

What will happen is, the blank record will get saved to database and displayed on list page as follows.

This is certainly not what you want right?
For validating the screen, Validate() method is provided in .NET API for EntryGroup object. This method validates the screen and fires the validations similar to that of COM API Validate method. Method returns True when all the validations are passed else returns False. If method returns False we have to retain the edit mode of the screen through code. Also in .NET API when validate returns false, new values entered on screen are lost which is not the expected behavior. This can be fixed through the code.
Below is the code snippet which handles the validations, navigations, mode as well as the retention of values on screen properly.
AddContent(HTML.StartTable());
AddContent(HTML.Form());
EntryGroup scrProductLine = new EntryGroup(“ProductLinesNewEntry”);
string hMode = Dispatch.EitherField(“HiddenMode”);
scrProductLine.Title = “New Product Line”;
if (hMode == “Save”)
{
 if (scrProductLine.Validate())
 {
  Record recProductLine = new Record(“ProductLine”);
  scrProductLine.FillEntityFromContent(recProductLine);
  recProductLine.SaveChanges();
  
  Dispatch.Redirect(UrlDotNet(ThisDotNetDll, “RunListPage”));
 }
 else
 {
  AddContent(HTML.InputHidden(“HiddenMode”, “”));
  AddContent(HTML.StartTable());
  Entity eProductLine = new Entity(“ProductLine”);
  scrProductLine.Fill(eProductLine);  
  AddContent((scrProductLine.GetHtmlInEditMode()));
  string sUrl = “javascript:document.EntryForm.HiddenMode.value=’Save’;”;
  AddSubmitButton(“Save”, “save.gif”, sUrl);
  AddUrlButton(“Cancel”, “cancel.gif”, UrlDotNet(ThisDotNetDll, “RunListPage”));
  AddContent(HTML.EndTable());
 }
}
else
{
 AddContent(HTML.InputHidden(“HiddenMode”, “”));
 AddContent(HTML.StartTable());
 AddContent((scrProductLine.GetHtmlInEditMode()));
 string sUrl = “javascript:document.EntryForm.HiddenMode.value=’Save’;”;
 AddSubmitButton(“Save”, “save.gif”, sUrl);
 AddUrlButton(“Cancel”, “cancel.gif”, UrlDotNet(ThisDotNetDll, “RunListPage”));
 AddContent(HTML.EndTable());
}