Error handling with a custom field control

Posted by Community Admin on 04-Aug-2018 17:37

Error handling with a custom field control

All Replies

Posted by Community Admin on 18-Oct-2016 00:00

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

This thread is closed