More in this section
Forums / Developing with Sitefinity / Error handling with a custom field control

Error handling with a custom field control

The forums are in read-only mode. In case that you want to directly contact the Progress Sitefinity team use the support center. In our Google Plus group you can find more than one thousand Sitefinity developers discussing different topics. For the Stack Overflow threads don’t forget to use the “Sitefinity” tag.
1 posts, 0 answered
  1. Carl
    Carl avatar
    2 posts
    Registered:
    09 Jun 2015
    18 Oct 2016
    Link to this post

    I’m working in Sitefinity 9.1 and I’m trying to implement Googles reCAPTCHA 2.0 (https://developers.google.com/recaptcha/intro) as a field control widget in Sitefinity. In the Global.asax.cs file, I’ve subscribed to the IFormsModuleEvent:

    if (e.CommandName == "Bootstrapped")
    {
        EventHub.Subscribe<IFormsModuleEvent>(evt => reCaptcha.reCaptcha_FormSavingEventHandler(evt));
    }

    Then, in the widget, I implemented the reCaptcha_FormSavingEventHandler: 

    public static void reCaptcha_FormSavingEventHandler(IFormsModuleEvent evt)
    {
        if (evt is FormValidatingEvent)
        {
            string reCaptchaResponse = SystemManager.CurrentHttpContext.Request.Form.GetValues("g-recaptcha-response").First();
     
            ReCaptchaResponse validatorResponse = ReCaptchaValidation.Validate(reCaptchaResponse);
            if (!validatorResponse.success)
            {
                throw new ValidationException(validatorResponse.errorCodes.First());
            }
        }
    }

    For good measure, here is my template:

    <%@ Control %>
    <%@ Register Assembly="Telerik.Sitefinity" TagPrefix="sf" Namespace="Telerik.Sitefinity.Web.UI" %>
     
    <script src="https://www.google.com/recaptcha/api.js" async defer></script>
    <div id="gCaptcha" runat="server" class="g-recaptcha" data-sitekey="xxx"></div>

     

    I’m stuck on how to get the error message to display where the widget is located. The error does display at the top of the form (highlighted in attachment), which is undesirable. I’d like it to appear below the widget itself. There isn't much else going on in the class, just logic for the template and script files (I'm not doing anything in the .js file; no client side validation and no data to store and/or retrieve).

    Does anyone have any ideas on how to accomplish this?

    Thanks!
    Carl
1 posts, 0 answered