volume_up

A critical vulnerability was discovered in React Server Components (Next.js). Our systems remain protected but we advise to update packages to newest version. Learn More

volume_up

A critical vulnerability was discovered in React Server Components (Next.js). Our systems remain protected but we advise to update packages to newest version. Learn More

Custom error message in XForm

Hello!

How can I show a custom error message in an XForm? I tried creating a Label and adding it to control.Controls collection but it didn't work. Now I've tried to do something like this

         

 //Validation of email addresses
            string emailAddress = e.FormData.GetValue("Email");
            string repeatEmailAddress = e.FormData.GetValue("RepeatEmail");

           
                if (!emailAddress.Equals(repeatEmailAddress))
                {
                    e.ErrorMessage = CurrentBlock.ErrorMessage;
                    e.CancelSubmit = true;
                }


but e.ErrorMessage is not displayed anywhere. Having a control to display errors in the markup of the block (not in the form) doesn't make a difference either since the properties are not updated for some reason. It's frustrating.  Can anyone help?

Thanks!

#132963
Aug 21, 2015 14:41
Vote:

Hello!

I found a solution that I publish here in case it helps somebody else in the same situation.

I had tried to add a Label to the controls collection with no success. The problem was that I was creating a EPiServer.XForms.WebControls.Label which for some reason couldn't be added. Changing to a System.Web.UI.WebControls.Label worked fine and the error message was displayed on the page. The code looks like this:

  public void XForm_BeforeSubmitPostedData(object sender, SaveFormDataEventArgs e)
        {
            XFormControl control = (XFormControl)sender;

            PageBase currentPage = control.Page as PageBase;

            if (currentPage == null)
            {
                return;
            }
            //We set the current page that the form has been posted from
            //This might differ from the actual page that the form property exists on.
            e.FormData.PageGuid = currentPage.CurrentPage.PageGuid;

            //Validation of email addresses
            string emailAddress = e.FormData.GetValue("Email");
            string repeatEmailAddress = e.FormData.GetValue("RepeatEmail");

            if (emailAddress != null && repeatEmailAddress != null)
            {
               if (!emailAddress.Equals(repeatEmailAddress))
                {
                    control.Controls.Add(CreateErrorLabel(CurrentBlock.ErrorMessage,"error"));
                    e.CancelSubmit = true;
                }
            }
        }

 And the CreateErrorLabel method:

private Label CreateErrorLabel(string labelText, string cssClass)
        {
            Label label = new Label();
            label.CssClass = cssClass;
            label.BackColor = Color.White;
            label.Text = labelText;

            return label;
        }



#133003
Aug 24, 2015 15:47
error This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.