2

I'm using a custom validator for ReCaptcha on Sitecore forms. I've followed all of the instructions in the Sitecore docs for creating the validator, field type, etc. Does anyone know why this override in the validator class is always null for value? Wondering if it is something simple that I missed?

public override ValidationResult Validate(object value)

Thanks!

4
  • Can you provide the code snippet for your -> public override ValidationResult Validate(object value)? May 1, 2022 at 9:03
  • ` public override ValidationResult Validate(object value) { if (value == null) { return ValidationResult.Success; } var isCaptchaValid = ValidateCaptcha((string)value, this.PrivateKey); if (!isCaptchaValid) { return new ValidationResult(this.FormatMessage(new object[] { this.Title })); } return ValidationResult.Success; } May 1, 2022 at 21:02
  • On the Recaptcha field in the form, did you select your custom Validator in the Validation Field? May 2, 2022 at 9:55
  • Yes, it is selected. May 3, 2022 at 0:54

2 Answers 2

2

You can try this recaptcha code, where you need to follow below steps:

  1. Create patch config for reCaptcha Sitekey and SecretKey
  2. Create new viewmodel class RecaptchaViewModel.cs
  3. Create new CustomRecaptcha.cshtml file
  4. Move to this location “Website/Views/FormBuilder/FieldTemplates” and open “Button.cshtm” and add “recaptcha” class in the class attribute.
  5. Add new template with name “CustomRecaptcha”
  6. Create new Field type here “/sitecore/system/Settings/Forms/Field Types/Security” with name “CustomRecaptcha”
  7. Custom reCaptcha Validation

More details is available here https://www.swatiguptablogs.com/2019/10/sitecore-9-forms-google-recaptcha-field.html

3
  • Thanks. My code is similar. No matter what I change value is always null. Maybe the question I should be asking is where does that value come from? Maybe I'm misunderstanding the flow. May 1, 2022 at 21:05
  • Please share your full code so it will be helpful to identify May 2, 2022 at 7:49
  • 1
    I ended up bailing on the custom validator approach and instead going with the custom submit action like Swati Gupta. I'm not crazy about changing the Button view, but out of time on this one. May 3, 2022 at 14:15
1

The value of the recaptcha field should be set manually as below

 var recaptchaResponse = grecaptcha.getResponse();
   $('#' + '@Html.IdFor(m => Model.Value)').val(recaptchaResponse);

Complete code for the FieldTemplate is here

@using Sitecore.ExperienceForms.Mvc.Html
@model KAO.Foundation.ExperienceForms.Extensions.GoogleRecaptcha

<script src='https://www.google.com/recaptcha/api.js'></script>
<div class="g-recaptcha" [email protected] data-callback="captchaCallback"></div>
<span class="field-validation-error" id="[email protected](m => Model.Value)" style="display:none">@Model.ErrorMessage</span>
<input id="@Html.IdFor(m => Model.Value)" name="@Html.NameFor(m => Model.Value)" class="@Model.CssClass" value="@Model.Value" type="hidden" data-sc-tracking="@Model.IsTrackingEnabled" data-sc-field-name="@Model.Name" @Html.GenerateUnobtrusiveValidationAttributes(m => m.Value) />
@Html.ValidationMessageFor(m => Model.Value)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        if ($('#' + '@Html.IdFor(m => Model.Value)').parent('form')) {
            $($('#' + '@Html.IdFor(m => Model.Value)').parent('form')).submit(function () {
                document.getElementById('captchaError-' + '@Html.IdFor(m => Model.Value)').style.display = "none";
                $('span[data-valmsg-for="@Html.NameFor(m => Model.Value)').hide();
                var recaptchaResponse = grecaptcha.getResponse();
                if (recaptchaResponse.length == 0) {
                    document.getElementById('captchaError-' + '@Html.IdFor(m => Model.Value)').value = $('#' + '@Html.IdFor(m => Model.Value)').data('val-required');
                    document.getElementById('captchaError-' + '@Html.IdFor(m => Model.Value)').style.display = "block";
                    return false;
                }
                else {
                    document.getElementById('captchaError-' + '@Html.IdFor(m => Model.Value)').style.display = "none";
                    $('#' + '@Html.IdFor(m => Model.Value)').val(recaptchaResponse);
                    return true;
                }
            });
        }
        function captchaCallback() {
            document.getElementById('captchaError-' + '@Html.IdFor(m => Model.Value)').style.display = "none";
            $('span[data-valmsg-for="@Html.NameFor(m => Model.Value)').hide();
            if (recaptchaResponse.length == 0) {
                document.getElementById('captchaError-' + '@Html.IdFor(m => Model.Value)').value = $('#' + '@Html.IdFor(m => Model.Value)').data('val-required');
                document.getElementById('captchaError-' + '@Html.IdFor(m => Model.Value)').style.display = "block";
            }
            else {
                document.getElementById('captchaError-' + '@Html.IdFor(m => Model.Value)').style.display = "none";
                $('#' + '@Html.IdFor(m => Model.Value)').val(recaptchaResponse);
            }
        }
    })


</script>   

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.