Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Namrata 8 posts 78 karma points
    19 days ago
    Namrata
    0

    ValidateSettings doesn't wait for frontend process to be completed

    Hello community,

    I have added an event listener on form submission. However, when the form is submitted it doesn't wait for the token generation and then in ValidateSettings -> context.Request.Form["g-recaptcha-response"] is null.

    Here is my custom field type code.

    @using Umbraco.Forms.Web
    @using Microsoft.Extensions.Configuration
    @model Umbraco.Forms.Web.Models.FieldViewModel
    @inject IConfiguration Configuration
    @{
        var siteKey = Configuration.GetSection("ReCaptchaEnterprise")["SiteKey"];
        if (!string.IsNullOrEmpty(siteKey))
        {
            Html.AddFormThemeScriptFile("https://www.google.com/recaptcha/enterprise.js?render=" + siteKey);
            Html.AddFormThemeScriptFile("https://www.google.com/recaptcha/api.js?render="+ siteKey);
            Html.AddFormThemeScriptFile("https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js");
            <div class="g-recaptcha"
                 data-sitekey="@siteKey"
                 data-size="invisible">
            </div>
    
    
    <script type="text/javascript">
                document.addEventListener('DOMContentLoaded', function(){
                    var formContainer = document.querySelector(".umbraco-forms-form");
                    var form = formContainer.querySelector("form");
                    form.addEventListener('submit', (event) => {
                        console.log(grecaptcha.execute());
                    }) 
                })       
            </script>
    
    
        }
        else
        {
            <p class="error">ERROR: reCAPTCHA is missing the Site Key. Please update the configuration to include a value.</p>
        }
    }
    

    Custom class for the custom field type in Umbraco :

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using AngleSharp.Io;
    using Microsoft.AspNetCore.Http;
    using Umbraco.Forms.Core.Enums;
    using Umbraco.Forms.Core.Models;
    using Umbraco.Forms.Core.Services;
    using Request = Org.BouncyCastle.Asn1.Ocsp.Request;
    
    namespace FormsExtensions
    {
        public class ReCaptchaEnterprise : Umbraco.Forms.Core.FieldType
        {
            public ReCaptchaEnterprise()
            {
                Id = new Guid(<guid>);
                Name = "ReCaptchaEnterprise";
                Description = "Render a custom reCaptcha enterprise field.";
                Icon = "icon-autofill";
                SortOrder = 10;
                FieldTypeViewName = "FieldType.ReCaptchaEnterprise.cshtml";
            }
    
            // Custom validation in here which will occur when the form is submitted.
            // Any strings returned will cause the submission to be considered invalid.
            // Returning an empty collection of strings will indicate that it's valid to proceed.
            public override IEnumerable<string> ValidateField(Form form, Field field, IEnumerable<object> postedValues, HttpContext context, IPlaceholderParsingService placeholderParsingService, IFieldTypeStorage fieldTypeStorage)
            {
                var returnStrings = new List<string>();
    
                var reCaptchaResponse =context.Request.Form["g-recaptcha-response"];
    
                Console.WriteLine("+++++++++++++++++++++ReCaptcha response+++++++++++++++++++++++++++" + reCaptchaResponse);
    
                // Also validate it against the default method (to handle mandatory fields and regular expressions)
                return base.ValidateField(form, field, postedValues, context, placeholderParsingService, fieldTypeStorage, returnStrings);
            }
        }    
    }
    

    Any insights are appreciated !

Please Sign in or register to post replies

Write your reply to:

Draft