jamahalwin
jamahalwin

Reputation: 1168

MVC3 ReCaptcha with Fluent Validation

I'm trying to validate ReCaptcha using FluentValidation but I'm having some issues. Even though the ReCaptcha.Validate returns true the ModelState is not valid. In order to validate the ReCaptcha I added a field in the view model. Before checking to see if the ModelState is valid I set the ReCaptcha field to whatever is returned from ReCaptcha.Validate.

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Index(Over18Model model)
    {
        model.ReCaptcha = Microsoft.Web.Helpers.ReCaptcha.Validate("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");

        if (ModelState.IsValid)
        {
            var table = new Prospect();
            dynamic o = new ExpandoObject();
            {
                o.FirstName = model.FirstName;
                o.LastName = model.LastName;
                o.Email = model.Email;
            }
            table.Save(o);
            return RedirectToAction("ThankYou", "Public");
        }
        ModelState.AddModelError(string.Empty, "Errors:  " + string.Join(" ; ", ModelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage)));
        return View(model); 
    }

    [Validator(typeof(Over18ModelValidator))]
    public class Over18Model
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public bool ReCaptcha { get; set; }
    }

    public class Over18ModelValidator : AbstractValidator<Over18Model>
    {
        public Over18ModelValidator()
        {
            RuleFor(x => x.FirstName).NotEmpty().WithMessage("First Name is Required");
            RuleFor(x => x.LastName).NotEmpty().WithMessage("Last Name is Required");
            RuleFor(x => x.Email).NotEmpty().WithMessage("Email is Required");
            RuleFor(x => x.Email).EmailAddress().WithMessage("Invalid Email");
            RuleFor(x => x.ReCaptcha).Equal(true).WithMessage("ReCaptcha error");
        }
    }

The Razor code looks like this...

<div class="signUpOver18">
    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()         
        @Form.TextBox(name: "FirstName", value: @Model.FirstName, labelText: "First Name:", containerClass: "fname", txtfldClass: "standard names", maxLength: 50) 
        @Form.TextBox(name: "LastName", value: @Model.LastName, labelText: "Last Name:", containerClass: "lname", txtfldClass: "standard names", maxLength: 100) 
        </div>
        @Form.Submit(value: "Enter The Sweepstakes", myClass: "btnSubmit")

        <div class="recaptchaHolder">
            @ReCaptcha.GetHtml(theme: "red", publicKey: "XXX-XXXX_XXXX-XX_") 
        </div>

        @Html.ValidationSummary(true)  
    }
</div>

Upvotes: 2

Views: 824

Answers (1)

Newbie
Newbie

Reputation: 361

If it is not mandatory to use fluentvalidation for validating captcha...try the microsoft web helpers..it works perfectly..

Upvotes: 0

Related Questions