+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Error handling with a custom field control

Error handling with a custom field control

1 posts, 0 answered
  1. Carl
    Carl avatar
    2 posts
    Registered:
    09 Jun 2015
    18 Oct
    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="6Lf4mwgUAAAAAGtvYwDaQ2oT4Dy9xSFOqupMNz92"></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