dejanstojanovic / MVC-Honeypot

Honeypot implementation in ASP.NET MVC
Other
12 stars 7 forks source link

ModelState is invalid even though model is correctly set. #2

Open niemyjski opened 8 years ago

niemyjski commented 8 years ago
        [StringLength(255)]
        [EmailAddress]
        [Required(ErrorMessage = "Email address is required.")]
        public string EmailAddress { get; set; }
        [HttpPost]
        [HoneypotFilter("EmailAddress")]
            @Html.HoneyPotField("EmailAddress", Model.EmailAddress, null, HtmlHelpers.InputType.Text, "email-address-validator", HtmlHelpers.InputType.Email)
 "Email Address": {
    "value": {
      "attempted_value": "",
      "culture": "en-US",
      "raw_value": [
        ""
      ]
    },
    "errors": [
      {
        "error_message": "Email address is required."
      }
    ]
  }

It appears that the action context is running after model state has been evaluated. Have you ran into this behavior? So far my short term work around is:

                if (!String.IsNullOrEmpty(cd.EmailAddress))
                    ModelState["Email Address"].Errors.RemoveWhere(e => e.ErrorMessage == "Email address is required.");
scgough commented 5 years ago

You can also add this before your if(!ModelState.IsValid) { //throw error } check:

    ModelState.Clear();
    TryValidateModel(YourModel);