Portal Engine Questions on portal engine and web parts.
Version 6.x > Portal Engine > BizForm Custom Validation add a new FieldErrorLabels View modes: 
User avatar
Member
Member
mark.wiseman-revium.com - 9/26/2012 12:35:20 AM
   
BizForm Custom Validation add a new FieldErrorLabels
I am trying to perform some custom validation in a text field if a particular value is selected from a drop down list.

The fields (that matter) in the form are
Source
- Allow Empty Value = false
- Form Control = Drop down list

OtherSource
- Allow Empty Value = true
- Form Control = Text box

If the value "other" is selected in Source i want to make OtherSource required

The problem is that Other Source does not have a FieldErrorLabels defined and when i add one it is not being displayed.

I have cloned bizform.ascx


protected void SetupControl()
{
//...
viewBiz.OnBeforeValidate += viewBiz_OnBeforeValidate;
//...
}

void viewBiz_OnBeforeValidate(object sender, EventArgs e)
{
var errorLabel = new LocalizedLabel();
errorLabel.Text = "Please enter Other Source value.";
errorLabel.Visible = true;
ViewBiz.BasicForm.FieldErrorLabels.Add("OtherSource", errorLabel);

ViewBiz.ValidationErrorMessage = "just show me something";
ViewBiz.StopProcessing = true;
}

Why cant i get the error label for OtherSource to show?

User avatar
Kentico Developer
Kentico Developer
kentico_ivanat - 9/27/2012 3:59:51 AM
   
RE:BizForm Custom Validation add a new FieldErrorLabels
Hi,

for this you could use advanced field editor which is available in the 7.0 version. How to configure depending fields is described in the article Using dependency fields in forms.

In the 6.0 version. The other source is a field in your form? Have you defined the error message for this field? Anyway, the simpliest solution is using the 7.0 version to configure this.

This way you do not need any customization.

Best regards,
Ivana Tomanickova

User avatar
Member
Member
mark.wiseman-revium.com - 9/27/2012 7:05:01 PM
   
RE:BizForm Custom Validation add a new FieldErrorLabels
Unfortunately 7.0 is not as option for us with this project.

The "OtherSource" field does have an error message associated with it but the LocalizedLabel is only in the FieldErrorLabels collection if "Allow empty value" is not ticked.

How can I insert a new LocalizedLabel into FieldErrorLabels during OnBeforeValidate, OnAfterValidate or OnBeforeSave so an error can be shown?

Thanks

User avatar
Kentico Developer
Kentico Developer
kentico_ivanat - 10/1/2012 5:50:42 AM
   
RE:BizForm Custom Validation add a new FieldErrorLabels
Hi,

here is the code for 6.0 version which shows you how to read and assign the error message to the top error message and to the field error message.

protected override void OnLoad(EventArgs e)
{
viewBiz.OnAfterValidate += new EventHandler(viewBiz_OnAfterValidate);
base.OnLoad(e);
}

void viewBiz_OnAfterValidate(object sender, EventArgs e)
{
// Read error message from one field
FormEngineUserControl feuc = (FormEngineUserControl)viewBiz.BasicForm.FieldControls["test"];
// Top error message
viewBiz.ValidationErrorMessage = feuc.ErrorMessage;

// Field error message
((CMS.ExtendedControls.LocalizedLabel)viewBiz.BasicForm.FieldErrorLabels["FirstName"]).Text = feuc.ErrorMessage;
}


Best regards,
Ivana Tomanickova