Google ReCaptcha 在未填写 Umbraco 自定义表单时不显示错误

Google ReCaptcha not showing error when It is not filled umbraco custom forms

我是 dot 的新手 net.I 尝试在 recaptha 未填写时显示错误消息,但 ModelState.AddModelError 未显示错误。所以我已经尝试过这种方式,但仍然不会在我的视图中显示错误

模型 我的模型文件

public class ContactFormViewModel{

    [Required(ErrorMessage ="Required")]
    public string FullName { get; set; }
    [Required(ErrorMessage = "Required")]
    public string Email { get; set; }
    [Required(ErrorMessage = "Required")]
    public string Subject { get; set; }
    public string Message { get; set; }
    public string ErrorMessageCaptcha { get; set; }     
    public string FeedbackField { get; set; }
    public string SubmittedFromUrl { get; set; }
}

控制器我的控制器文件

public ActionResult SubmitFormAsync(ContactFormViewModel submittedForm)
    {


        RecaptchaVerificationHelper recaptchaHelper = this.GetRecaptchaVerificationHelper();
        if (string.IsNullOrEmpty(recaptchaHelper.Response))
        {
            // ModelState.AddModelError(string.Empty, "Please complete the reCAPTCHA");
            //  ModelState.AddModelError("reCAPTCHA", "The reCAPTCHA is incorrect");

            submittedForm.ErrorMessageCaptcha = "Email not found or matched";
            //  return CurrentUmbracoPage();
            return PartialView(submittedForm);
            // return View();
        }
        else
        {
            RecaptchaVerificationResult recaptchaResult = recaptchaHelper.VerifyRecaptchaResponse();
            if (recaptchaResult != RecaptchaVerificationResult.Success)
            {

                //  ModelState.AddModelError("reCAPTCHA", "Please complete the reCAPTCHA");
                //ModelState.AddModelError(string.Empty, "The reCAPTCHA is incorrect");
                submittedForm.ErrorMessageCaptcha = "Email not found or matched1";
                //  return CurrentUmbracoPage();
                return PartialView(submittedForm);
                // return View();
            }
        } 

        //FeedbackField is Honeypot captcha
        if (!ModelState.IsValid || !string.IsNullOrEmpty(submittedForm.FeedbackField))
        {}

查看我的查看文件

 <div class="form-group">
            <input type="hidden" class="hidden" name="PageId" value="@currentPage.Id" />
            @Html.Recaptcha(theme: Recaptcha.Web.RecaptchaTheme.Clean)

            <p class="error-message">@Model.ErrorMessageCaptcha</p>
            @*@if (ViewData.ModelState.IsValid)
            {

                @Html.ValidationSummary()

            }*@


            @Html.Label("error message", new { style = "display:none", id = "recaptchaErrorMessage" })

            <button type="submit" id="btn-form-submit" class="btn-secondary pull-right">@currentPage.GetPropertyValue("buttonText")</button>
        </div>

不确定您使用的是什么版本或 recaptcha,但我已经在我构建的许多自定义表单上使用了它,如果我是对的,recaptcha 按钮会添加一个额外的输入,当提交表单时被添加到您的表单数据中。所以我在我的表格上所做的只是检查那个额外的字段并创建我自己的简单错误消息和 return 有错误的当前页面,如下所示:

var formData = Request.Form;
        var captchaRequest = formData["g-recaptcha-response"];
        if (string.IsNullOrWhiteSpace(captchaRequest) || !ModelState.IsValid)
        {
            TempData["contactError"] = "Ops... Form Error, There was an error with your details please check them and try again.";
            return CurrentUmbracoPage();
        }

请记住,即使没有填写 recaptcha,ModelState 也将有效,在我的例子中,它只是捕获所有消息。

试试这个。验证工作正常。替换这些行

Controller

RecaptchaVerificationHelper recaptchaHelper = this.GetRecaptchaVerificationHelper();
            if (string.IsNullOrEmpty(recaptchaHelper.Response))
            {
                ModelState.AddModelError("reCAPTCHA", "Please complete the reCAPTCHA");
                return CurrentUmbracoPage();
            }
            else
            {
                RecaptchaVerificationResult recaptchaResult = recaptchaHelper.VerifyRecaptchaResponse();
                if (recaptchaResult != RecaptchaVerificationResult.Success)
                {
                    ModelState.AddModelError("reCAPTCHA", "The reCAPTCHA is incorrect");
                    return CurrentUmbracoPage();
                }
            }

View

       <div class="form-group col-sm-12">
                @Html.Recaptcha(theme: Recaptcha.Web.RecaptchaTheme.Clean)

                @{
                    var errors = ViewData.ModelState.Values.SelectMany(v => v.Errors);
                }
                @if (errors != null && errors.Any())
                {
                    @Html.Label(errors.FirstOrDefault().ErrorMessage, new { id = "recaptchaErrorMessage" })
                }
            </div>