0

I followed the below 2 posts to create a custom field for Recaptcha in Sitecore forms.

https://sitecorescientist.com/2018/11/11/sitecore-9-forms-part-2-google-recaptcha-field/ http://blog.nikkipunjabi.com/2017/12/sitecore-9-forms-recaptcha-form-element.html

Faced multiple issues, resolved them, and now stuck in the final issue.

The formbuilder is not able to post data. The page just refreshes on submit. There are no console errors. The value is set for all the fields in the form.

This is how my form html looks like.

<form action="/formbuilder?sc_site=KeystoneAutomotive&amp;fxb.FormItemId=8fcd3ff0-b371-4f9a-bd76-5d035bdfe423&amp;fxb.HtmlPrefix=fxb.3eca5dfc-c3fb-4067-ac9b-bb7caebf4d6e" data-ajax="true" data-ajax-method="Post" data-ajax-mode="replace-with" data-ajax-success="$.validator.unobtrusive.parse('#fxb_3eca5dfc-c3fb-4067-ac9b-bb7caebf4d6e_8fcd3ff0-b371-4f9a-bd76-5d035bdfe423');$.fxbFormTracker.texts=$.fxbFormTracker.texts||{};$.fxbFormTracker.texts.expiredWebSession='Your session has expired. Please refresh this page.';$.fxbFormTracker.parse('#fxb_3eca5dfc-c3fb-4067-ac9b-bb7caebf4d6e_8fcd3ff0-b371-4f9a-bd76-5d035bdfe423');$.fxbConditions.parse('#fxb_3eca5dfc-c3fb-4067-ac9b-bb7caebf4d6e_8fcd3ff0-b371-4f9a-bd76-5d035bdfe423')" data-ajax-update="#fxb_3eca5dfc-c3fb-4067-ac9b-bb7caebf4d6e_8fcd3ff0-b371-4f9a-bd76-5d035bdfe423" data-sc-fxb="8fcd3ff0-b371-4f9a-bd76-5d035bdfe423" enctype="multipart/form-data" id="fxb_3eca5dfc-c3fb-4067-ac9b-bb7caebf4d6e_8fcd3ff0-b371-4f9a-bd76-5d035bdfe423" method="post"><input id="fxb_3eca5dfc-c3fb-4067-ac9b-bb7caebf4d6e_FormSessionId" name="fxb.3eca5dfc-c3fb-4067-ac9b-bb7caebf4d6e.FormSessionId" type="hidden" value="cdf49bfe-4826-4af9-95bc-1d7cb912dbf4"><input id="fxb_3eca5dfc-c3fb-4067-ac9b-bb7caebf4d6e_IsNewSession" name="fxb.3eca5dfc-c3fb-4067-ac9b-bb7caebf4d6e.IsNewSession" type="hidden" value="0"><input name="__RequestVerificationToken" type="hidden" value="K46tRixH5db3May00OkywTEJAsV5LcTzVGQ9fI6ye8HDEh7fbN0fl0Ol7WpOtPEUhoqIiwO42L67zDJcUMf4bEmyBs0M_g1g7xpZc-0t8vs1"><input id="fxb_3eca5dfc-c3fb-4067-ac9b-bb7caebf4d6e_FormItemId" name="fxb.3eca5dfc-c3fb-4067-ac9b-bb7caebf4d6e.FormItemId" type="hidden" value="8fcd3ff0-b371-4f9a-bd76-5d035bdfe423"><input id="fxb_3eca5dfc-c3fb-4067-ac9b-bb7caebf4d6e_PageItemId" name="fxb.3eca5dfc-c3fb-4067-ac9b-bb7caebf4d6e.PageItemId" type="hidden" value="65ab4e49-3f27-417f-9855-9d852fbea35b">
<input id="fxb_3eca5dfc-c3fb-4067-ac9b-bb7caebf4d6e_Fields_Index_4594b5a6-69e3-4381-81b5-17db2e1d0e60" name="fxb.3eca5dfc-c3fb-4067-ac9b-bb7caebf4d6e.Fields.Index" type="hidden" value="4594b5a6-69e3-4381-81b5-17db2e1d0e60"><input id="fxb_3eca5dfc-c3fb-4067-ac9b-bb7caebf4d6e_Fields_4594b5a6-69e3-4381-81b5-17db2e1d0e60__ItemId" name="fxb.3eca5dfc-c3fb-4067-ac9b-bb7caebf4d6e.Fields[4594b5a6-69e3-4381-81b5-17db2e1d0e60].ItemId" type="hidden" value="4594b5a6-69e3-4381-81b5-17db2e1d0e60"><label for="fxb_3eca5dfc-c3fb-4067-ac9b-bb7caebf4d6e_Fields_4594b5a6-69e3-4381-81b5-17db2e1d0e60__Value" class="">Label</label>
<input id="fxb_3eca5dfc-c3fb-4067-ac9b-bb7caebf4d6e_Fields_4594b5a6-69e3-4381-81b5-17db2e1d0e60__Value" name="fxb.3eca5dfc-c3fb-4067-ac9b-bb7caebf4d6e.Fields[4594b5a6-69e3-4381-81b5-17db2e1d0e60].Value" class="" type="text" value="" maxlength="255" placeholder="" data-sc-tracking="True" data-sc-field-name="Single-Line Text" data-sc-field-key="B8F234DBCB8147DAB63D61F9DB89FBB1">
<span class="field-validation-valid" data-valmsg-for="fxb.3eca5dfc-c3fb-4067-ac9b-bb7caebf4d6e.Fields[4594b5a6-69e3-4381-81b5-17db2e1d0e60].Value" data-valmsg-replace="true"></span><input id="fxb_3eca5dfc-c3fb-4067-ac9b-bb7caebf4d6e_Fields_Index_1da18a5b-eb80-4d8a-a860-6cad753afca9" name="fxb.3eca5dfc-c3fb-4067-ac9b-bb7caebf4d6e.Fields.Index" type="hidden" value="1da18a5b-eb80-4d8a-a860-6cad753afca9"><input id="fxb_3eca5dfc-c3fb-4067-ac9b-bb7caebf4d6e_Fields_1da18a5b-eb80-4d8a-a860-6cad753afca9__ItemId" name="fxb.3eca5dfc-c3fb-4067-ac9b-bb7caebf4d6e.Fields[1da18a5b-eb80-4d8a-a860-6cad753afca9].ItemId" type="hidden" value="1da18a5b-eb80-4d8a-a860-6cad753afca9"><script src="https://www.google.com/recaptcha/api.js"></script>
<div class="g-recaptcha" data-sitekey="6LdKA9YlAAAAAADp3KwI0LicyL_YIiROXzhppD0o" data-callback="captchaCallback"><div style="width: 304px; height: 78px;"><div><iframe title="reCAPTCHA" src="https://www.google.com/recaptcha/api2/anchor?ar=1&amp;k=6LdKA9YlAAAAAADp3KwI0LicyL_YIiROXzhppD0o&amp;co=aHR0cHM6Ly9sb2NhbC5rZXlzdG9uZWF1dG9tb3RpdmUuY29tOjQ0Mw..&amp;hl=en-GB&amp;v=wqcyhEwminqmAoT8QO_BkXCr&amp;size=normal&amp;cb=19u5cmrwok46" width="304" height="78" role="presentation" name="a-wagvc0c71l4g" frameborder="0" scrolling="no" sandbox="allow-forms allow-popups allow-same-origin allow-scripts allow-top-navigation allow-modals allow-popups-to-escape-sandbox"></iframe></div><textarea id="g-recaptcha-response-1" name="g-recaptcha-response" class="g-recaptcha-response" style="width: 250px; height: 40px; border: 1px solid rgb(193, 193, 193); margin: 10px 25px; padding: 0px; resize: none; display: none;"></textarea></div><iframe style="display: none;"></iframe></div>
<span class="field-validation-error" id="captchaError-fxb_3eca5dfc-c3fb-4067-ac9b-bb7caebf4d6e_Fields_1da18a5b-eb80-4d8a-a860-6cad753afca9__Value" style="display:none">Error in captcha</span>

<input id="fxb_3eca5dfc-c3fb-4067-ac9b-bb7caebf4d6e_Fields_1da18a5b-eb80-4d8a-a860-6cad753afca9__Value" name="fxb.3eca5dfc-c3fb-4067-ac9b-bb7caebf4d6e.Fields[1da18a5b-eb80-4d8a-a860-6cad753afca9].Value" class="" value="03AL8dmw__R-Ywf0xMXsnyOx85qQisrJmgpb8Z6xUer76DJ3SDcdOkFXWpNNxF2gu4FjM3qEIkbRBLJTiuaTHzUVAaCokYpSJenuwjjkBCVSXZZbtVi_JkjFC5cTApwCYLn-IMnV-nUc-0opiAEJ5MVccAYO1pIloYsO643_i5yt-Nr1ReXKWIKqSbtV-hAWRQUksDAfOZflnUpIOBXE2EPWwpunBgDNUqIp3w-cZZeKj8jcNLs4VtBJ5qWxQfFQC5edgnL1oZIZpyHuQJK3W3NbRtn-RIsPcVq4wsTaZsurj51qFVyUW4oiL02_e__1pGcv8JJk8OI4r3v90meKuy3H0Pe7j_Pr-OljQKdp20UsvDPeNObEHK41v5QdwsosdoeICQvJbL8O2dY7fX_KU_mGgS8KWCBGsRHSJkHUu-Qow9gEmeUIrmabaLeunjYT88uNpT6gYyL3gXiCqcvSFDXijnqDOrM39P5xIUNCy_eKk3-gJrGIU38SLuxtELlETgvNCVxzRbD4AsVOr1jn8S5F8f8NUm3h7qWZw2-7MdgVvh4fjv7CQYVZw5A-wFqs_U5nfcGv8Jj2SmX5On9m1JzP_843tNz27VnTvf2zOW4BX4xIWtGYNHT5tywnqgfHlL-dKbgPZy-2pGqjV1rH0ipmaTBW7eQt0Mus_IQGEH-Ndsi0--Jd38HpjfSeBHcMU3w2xIlNsNXzL2-LPaRFZslU3lcA0EeE3b8BvG6uBvF9pCVy3ad-xBc_wgXt4wZrI-mGiwI8iWe1YBYkDjPYvp-YhvmEeXOR31ZGG6GXMWDlaSRD9N7IAu33kbf6_ZAajYjbuPBF1QdwTpX4trXJ14a3l7GTrExTj0KpHN9I0Z13Pf2QkAgZvRSe4An_OEXdaA6FcPm3MSLfQBlgpXo8EOWF7m-VEMX1bkHt2Fiy4uq-D6E2f2qrO7a42v0F4Y89pTfjA8i5w4lPOkkq8t14JA_5jmSFLTIyIxIJ-3QMbB7dvwyNSZeGP7rR6GHMOQLjnkLpvXNT5CZdzedETyeGe2CtD0M9XwKR-NwKnKBw2VVHFb7lD2T_WaaZd0jx4MTiMIZgxm9zOoW63pZAhspn-T3f-fBjH3mtFIlJFVN3oBrYcdlmm-BrOwDC1lajbmUcFWyQSAkMkROtNoSzK8SD0BjZRxc6w8zAgb1VwWQ669qdEKFWFSnL3j2MC4Gb4yKyQrjnVfUR-aBZp1xObLgf9ETlopE3nY9l69U44lu4orcWk2Vj85ufgT15SQtzgnGWXjx7TNdmoU31rzQ-hFyVOm6AsR10Wfx52Fv1DkdPTba5ONTCF5jX-PodEI4xpcu6G5S2sN3kOUhPQhUS08-83hbueMrhvlZ0_3i6uiDc7g5m8oolc0bQz11HTnLtawG9D_9Yn6YijjjpZpg8T-xLDli0zfLxkx_i2weR72QhygHJdQ4mnp6fSkcqBeU8TN7AM5iSIZ61eM21J2fwdJxX52HsWFkvHxPvPNGiNdnPuIkHqG_5SjGvsyLf4ZpraTCh7q87JtF82bBEXvID7P9kJTuHfsFQqVgxJAYI8QBjDe3hoFOvoMJKb0AWNXGg9hHB3ckSpn4pGbBFvbsvwargjCAWmPEqxrkDt8MJXxrqE3b8KUCjl-L-ZUcMmWT4NqYNqwDYvuHIFUfFEhT5iZczxa7QZ6LCP8TZ3z4z9anWxB9Ikr7pa2kmycfBV5EcDDwCLBfha2uB0pkwM-" type="hidden" data-sc-field-key="98C8B3BDD1954CBC9874AB1C9C5D012B" data-sc-tracking="True" data-sc-field-name="ReCaptcha">
<span class="field-validation-valid" data-valmsg-for="fxb.3eca5dfc-c3fb-4067-ac9b-bb7caebf4d6e.Fields[1da18a5b-eb80-4d8a-a860-6cad753afca9].Value" data-valmsg-replace="true" style="display: none;"></span>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

<script>
    $(document).ready(function () {
        console.log($('#' + 'fxb_3eca5dfc-c3fb-4067-ac9b-bb7caebf4d6e_Fields_1da18a5b-eb80-4d8a-a860-6cad753afca9__Value').parent('form'))
        console.log($('#' + 'fxb_3eca5dfc-c3fb-4067-ac9b-bb7caebf4d6e_Fields_1da18a5b-eb80-4d8a-a860-6cad753afca9__Value'))
        if ($('#' + 'fxb_3eca5dfc-c3fb-4067-ac9b-bb7caebf4d6e_Fields_1da18a5b-eb80-4d8a-a860-6cad753afca9__Value').parent('form')) {
            $($('#' + 'fxb_3eca5dfc-c3fb-4067-ac9b-bb7caebf4d6e_Fields_1da18a5b-eb80-4d8a-a860-6cad753afca9__Value').parent('form')).submit(function () {
                document.getElementById('captchaError-' + 'fxb_3eca5dfc-c3fb-4067-ac9b-bb7caebf4d6e_Fields_1da18a5b-eb80-4d8a-a860-6cad753afca9__Value').style.display = "none";
                $('span[data-valmsg-for="fxb.3eca5dfc-c3fb-4067-ac9b-bb7caebf4d6e.Fields[1da18a5b-eb80-4d8a-a860-6cad753afca9].Value').hide();
                var recaptchaResponse = grecaptcha.getResponse();
                if (recaptchaResponse.length == 0) {
                    document.getElementById('captchaError-' + 'fxb_3eca5dfc-c3fb-4067-ac9b-bb7caebf4d6e_Fields_1da18a5b-eb80-4d8a-a860-6cad753afca9__Value').value = $('#' + 'fxb_3eca5dfc-c3fb-4067-ac9b-bb7caebf4d6e_Fields_1da18a5b-eb80-4d8a-a860-6cad753afca9__Value').data('val-required');
                    document.getElementById('captchaError-' + 'fxb_3eca5dfc-c3fb-4067-ac9b-bb7caebf4d6e_Fields_1da18a5b-eb80-4d8a-a860-6cad753afca9__Value').style.display = "block";
                    return false;
                }
                else {
                    document.getElementById('captchaError-' + 'fxb_3eca5dfc-c3fb-4067-ac9b-bb7caebf4d6e_Fields_1da18a5b-eb80-4d8a-a860-6cad753afca9__Value').style.display = "none";
                    $('#' + 'fxb_3eca5dfc-c3fb-4067-ac9b-bb7caebf4d6e_Fields_1da18a5b-eb80-4d8a-a860-6cad753afca9__Value').val(recaptchaResponse);
                    alert('9')
                    return true;
                }
            });
        }
    });

    function captchaCallback() {
        document.getElementById('captchaError-' + 'fxb_3eca5dfc-c3fb-4067-ac9b-bb7caebf4d6e_Fields_1da18a5b-eb80-4d8a-a860-6cad753afca9__Value').style.display = "none";
        $('span[data-valmsg-for="fxb.3eca5dfc-c3fb-4067-ac9b-bb7caebf4d6e.Fields[1da18a5b-eb80-4d8a-a860-6cad753afca9].Value').hide();
        var recaptchaResponse = grecaptcha.getResponse();
        if (recaptchaResponse.length == 0) {
            document.getElementById('captchaError-' + 'fxb_3eca5dfc-c3fb-4067-ac9b-bb7caebf4d6e_Fields_1da18a5b-eb80-4d8a-a860-6cad753afca9__Value').value = $('#' + 'fxb_3eca5dfc-c3fb-4067-ac9b-bb7caebf4d6e_Fields_1da18a5b-eb80-4d8a-a860-6cad753afca9__Value').data('val-required');
            document.getElementById('captchaError-' + 'fxb_3eca5dfc-c3fb-4067-ac9b-bb7caebf4d6e_Fields_1da18a5b-eb80-4d8a-a860-6cad753afca9__Value').style.display = "block";
        }
        else {
            document.getElementById('captchaError-' + 'fxb_3eca5dfc-c3fb-4067-ac9b-bb7caebf4d6e_Fields_1da18a5b-eb80-4d8a-a860-6cad753afca9__Value').style.display = "none";
            alert($('#' + 'fxb_3eca5dfc-c3fb-4067-ac9b-bb7caebf4d6e_Fields_1da18a5b-eb80-4d8a-a860-6cad753afca9__Value').val())
            $('#' + 'fxb_3eca5dfc-c3fb-4067-ac9b-bb7caebf4d6e_Fields_1da18a5b-eb80-4d8a-a860-6cad753afca9__Value').val(recaptchaResponse);
            alert($('#' + 'fxb_3eca5dfc-c3fb-4067-ac9b-bb7caebf4d6e_Fields_1da18a5b-eb80-4d8a-a860-6cad753afca9__Value').val())
        }
    }
</script>



<input value="Text" type="submit" class="" name="fxb.3eca5dfc-c3fb-4067-ac9b-bb7caebf4d6e.a53f4002-2820-4705-8ab0-70f0ec471fde" data-sc-field-key="81A15D37099B4CB48ABBB024A9D9745C">
<input id="fxb_3eca5dfc-c3fb-4067-ac9b-bb7caebf4d6e_NavigationButtons" name="fxb.3eca5dfc-c3fb-4067-ac9b-bb7caebf4d6e.NavigationButtons" type="hidden" value="a53f4002-2820-4705-8ab0-70f0ec471fde">
<input id="fxb_3eca5dfc-c3fb-4067-ac9b-bb7caebf4d6e_a53f4002-2820-4705-8ab0-70f0ec471fde" name="fxb.3eca5dfc-c3fb-4067-ac9b-bb7caebf4d6e.a53f4002-2820-4705-8ab0-70f0ec471fde" type="hidden" value="0">        <input type="hidden" data-sc-fxb-condition="" value="{}">
</form>

Any help is highly appreciated!!!

7
  • Did you get any error in browser console? Commented May 12, 2023 at 13:43
  • Do you see the post request in the network tab of the browser developer tool, upon submit button click? Commented May 12, 2023 at 14:43
  • No console errors @Sumit Commented May 12, 2023 at 15:20
  • @Sandeep - I dont see any post request in the network tab. As soon as I submit, the page refreshes Commented May 12, 2023 at 15:21
  • If you remove your custom recaptcha form element from form, are you able to submit? This is to cross check if you have issue with recaptcha implementation. Commented May 13, 2023 at 2:47

1 Answer 1

1

Below are the steps you can follow:

  • Create patch config for reCaptcha Sitekey and SecretKey
 <?xml version="1.0"?>  
 <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/">  
  <sitecore>  
   <settings>  
 <setting name="ReCaptchaSiteKey" value="site-key" />  
    <setting name="ReCaptchaSecretKey" value="secret-key" />  </settings>  
 </sitecore>  
 </configuration>  
  • Create new viewmodel class RecaptchaViewModel.cs
[Serializable()]  
   public class RecaptchaViewModel : MultipleLineTextViewModel  
   {  
     public string SiteKey { get; set; }  
     public string SecretKey { get; set; }  
     public string Message { get; set; }  
     protected override void InitItemProperties(Item item)  
     {  
       base.InitItemProperties(item);  
       string GoogleSiteKey = Configuration.Settings.GetSetting("ReCaptchaSiteKey");  
       string GoogleSecretKey = Configuration.Settings.GetSetting("ReCaptchaSecretKey");  
       if (!string.IsNullOrEmpty(GoogleSiteKey) && !string.IsNullOrEmpty(GoogleSecretKey))  
       {  
         SiteKey = GoogleSiteKey;  
         SecretKey = GoogleSecretKey;  
       }  
     }  
   }  
  • Create new CustomRecaptcha.cshtml file on this location “Website/Views/FormBuilder/FieldTemplates”
@model <Path>.RecaptchaViewModel  
 <script src='https://www.google.com/recaptcha/api.js'></script>  
 <script>  
   $(document).ready(function () {  
     $('.recaptcha').click(function (e) {  
       var recaptchaResponse = grecaptcha.getResponse();  
       console.log(recaptchaResponse);  
       console.log(recaptchaResponse.length);  
       if (recaptchaResponse.length != 0) {  
         $('.googleRecaptchaClass').val(recaptchaResponse);  
       }  
     })  
   });  
 </script>  
 <span class="msg-error"></span>  
 <textarea id="@Html.IdFor(m => Model.Value)" class="googleRecaptchaClass" style="display:none;" name="@Html.NameFor(m => Model.Value)" data-sc-tracking="@Model.IsTrackingEnabled" data-sc-field-name="@Model.Name"></textarea>  
 <div class="g-recaptcha" data-sitekey="@Model.SiteKey"></div>  
  • Move to this location “Website/Views/FormBuilder/FieldTemplates” and open “Button.cshtm” and add “recaptcha” class in class attribute.

enter image description here

  • In the Sitecore Content Editor, navigate to /sitecore/templates/System/Forms/Fields
  • Then add new template with name “CustomRecaptcha” and select Base template /sitecore/templates/System/Templates/Template
  • You can inherit template which is required
  • Switch to Core DB and create copy of this item “sitecore/client/Applications/FormsBuilder/Components/Layouts/PropertyGridForm/PageSettings/Settings/SingleLineText” with name “CustomRecaptcha”
  • Now move again to Master DB.
  • Create new Field type here “/sitecore/system/Settings/Forms/Field Types/Security” with name “CustomRecaptcha”
  • Fill below details: View Path: FieldTemplates/CustomRecaptcha Model Type: . RecaptchaViewModel, Property Editor: Property Editor Settings/CustomRecaptcha Field Template: Fields/CustomRecaptcha

Custom reCaptcha Validation

  • Create new submit action class RecaptchaValidate
public class RecaptchaValidate : SubmitActionBase<string>  
   {  
     /// <summary>  
     /// Initializes a new instance of the <see cref="LogSubmit"/> class.  
     /// </summary>  
     /// <param name="submitActionData">The submit action data.</param>  
     public RecaptchaValidate(ISubmitActionData submitActionData) : base(submitActionData)  
     {  
     }  
     /// <summary>  
     /// Tries to convert the specified <paramref name="value" /> to an instance of the specified target type.  
     /// </summary>  
     /// <param name="value">The value.</param>  
     /// <param name="target">The target object.</param>  
     /// <returns>  
     /// true if <paramref name="value" /> was converted successfully; otherwise, false.  
     /// </returns>  
     protected override bool TryParse(string value, out string target)  
     {  
       target = string.Empty;  
       return true;  
     }  
     /// <summary>  
     /// Executes the action with the specified <paramref name="data" />.  
     /// </summary>  
     /// <param name="data">The data.</param>  
     /// <param name="formSubmitContext">The form submit context.</param>  
     /// <returns>  
     ///  <c>true</c> if the action is executed correctly; otherwise <c>false</c>  
     /// </returns>  
     protected override bool Execute(string data, FormSubmitContext formSubmitContext)  
     {  
       if (!formSubmitContext.HasErrors)  
       {  
         RecaptchaViewModel googleRecaptchaFieldValue = formSubmitContext.Fields.Where(x => x is RecaptchaViewModel).FirstOrDefault() as RecaptchaViewModel;  
         if (googleRecaptchaFieldValue != null)  
         {  
           ISitecoreContext sitecoreContext = ServiceLocator.ServiceProvider.GetService<ISitecoreContext>();  
           var formSettings = sitecoreContext.GetItem<_FormSettings>(ItemIds.FormSettings);  
           if (formSettings != null && !string.IsNullOrEmpty(formSettings.SecretKey))  
           {  
             bool isCapthcaValid = ValidateCaptcha(googleRecaptchaFieldValue.Value, formSettings.SecretKey.ToString());  
             try  
             {  
               if (isCapthcaValid)  
               {  
                 return true;  
               }  
             }  
             catch  
             {  
               formSubmitContext.Abort();  
               return false;  
             }  
           }  
         }  
         formSubmitContext.Abort();  
         return false;  
       }  
       else  
       {  
         Logger.Warn(Invariant($"Form {formSubmitContext.FormId} submitted with errors: {string.Join(", ", formSubmitContext.Errors.Select(t => t.ErrorMessage))}."), this);  
       }  
       formSubmitContext.Abort();  
       return false;  
     }  
     public static bool ValidateCaptcha(string response, string secret)  
     {  
       string RecaptchaUrl = Configuration.Settings.GetSetting("GoogleApi");  
       var client = new WebClient();  
       var reply = client.DownloadString(RecaptchaUrl+"?secret=" + secret + "&response="+response);  
       var captchaResponse = JsonConvert.DeserializeObject<CaptchaResponse>(reply);  
       return System.Convert.ToBoolean(captchaResponse.Success);  
     }  
   }  
  • ItemIds.FormSettings is Sitecore item id which contains SecretKey field

  • For GoogleApi key create a patch file like below:

 <?xml version="1.0"?>  
 <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/">  
  <sitecore>  
   <settings>  
    <setting name="GoogleApi" value="https://www.google.com/recaptcha/api/siteverify"/>  
   </settings>    
  </sitecore>  
 </configuration>  
  • Create CaptchaResponse class
public class CaptchaResponse  
   {  
     [JsonProperty("success")]  
     public bool Success { get; set; }  
     [JsonProperty("error-codes")]  
     public List<string> ErrorMessage { get; set; }  
   }  
  • Go to this location “/sitecore/system/Settings/Forms/Submit Actions”

  • Create new submit action “Recaptcha Validate” using this template “/sitecore/templates/System/Forms/Submit Action”

  • Fill below details: Model Type: .RecaptchaValidate Error Message: Invalid Captcha

  • Now go to Sitecore Form Editor and you will see Custom Captcha control inside Security tab enter image description here

  • Drag and drop control into your form enter image description here

  • Do not forget to select “Recaptcha Validate” on your submit action

enter image description here

  • Render form into your page

More details you can check here https://swatiguptablogs.blogspot.com/2019/10/sitecore-9-forms-google-recaptcha-field.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.