Force form control to failed validation in Kentico BizForm - Code Behind

This is an example to manually force stop the biz form from continue processing save data and show error message to the Field Form Control.

Note this is just an example. You may use this approach to set very complex validation on the Field in BizForm.


OnLoad Method

   protected override void OnLoad(EventArgs e)  
{
viewBiz.OnAfterSave += viewBiz_OnAfterSave;
viewBiz.OnBeforeSave += viewBiz_OnBeforeSave;
base.OnLoad(e);
}



OnBeforeSave Method

  void viewBiz_OnBeforeSave(object sender, EventArgs e)  
{
//get the field form controller
FormEngineUserControl fieldFormController = viewBiz.FieldControls["<field name>"];
if (fieldFormController != null)
{
//do custom validation - example fieldFormControl dont have text "*"
if (!fieldFormController.Text.Contains("*"))
{
//stop biz form from continue processing
viewBiz.StopProcessing = true;
//focus the field / form controller
fieldFormController.Focus();
//show error message
viewBiz.DisplayErrorLabel("<field name>", "Field must have character \"*\"");
}
}
}


Popular posts from this blog

How to create zip file to download in JSP- Servlet

How to create DataGrid or GridView in JSP - Servlet

Pinging in ASP.NET Example