2
FluentValidationを使用してReCaptchaを検証しようとしていますが、いくつか問題があります。 ReCaptcha.Validateがtrueを返しても、ModelStateは無効です。 ReCaptchaを検証するために、私はビューモデルでフィールドを追加しました。 ModelStateが有効かどうかを調べる前に、ReCaptchaフィールドをReCaptcha.Validateから返されるものに設定します。Fluent Validationを使用したMVC3 ReCaptcha
[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");
}
}
カミソリのコードは次のようになります...
<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>
これは機能しませんか? – Romias