0

I've read from other posting, if a required field validator is hidden, javascript (client-side) does not validate the control as expected, however server-side is unaware that the control is hidden and thus validates the validator as false. Not sure why server-side is not notified on postback, but will accept that that's the way it is.

So, problem is the ModelState.IsValid is false because of these hidden validators. I figured no problem, I know the ModelState keys for these hidden validators, so I'll just remove the errors and set the ValidationState to Valid. It seems this does not work as the ErrorCount did not change and ModelState.IsValid is still false. I used the following code:

 modelState["ModelVM.MyKey"].Errors.Clear();
 modelState["ModelVM.MyKey"].ValidationState = ModelValidationState.Valid;

Do I need to refresh the ModelState? Or is there a better, cleaner way to deal with hidden required field validators? Any help would be greatly appreciated.

I'm using ASP.NET Core 2.0. Thanks.

  • You can implement RequiredIf attribute to exclude the property for validation for dependency property provided in RequiredIf. Check the blog http://blogs.msdn.com/b/simonince/archive/2011/02/04/conditional-validation-in-asp-net-mvc-3.aspx – user1672994 Jun 01 '18 at 07:58
  • What error message are you getting from the ModelState? – ZerosAndOnes Jun 01 '18 at 08:00
  • user1672994: The problem is server-side. Client-side is working as expected. I believe the blog posting is related to registering custom javascript validator with the jquery-validation-unobtrusive javascript. – user3602841 Jun 01 '18 at 08:08
  • I believe I posted the wrong link - https://stackoverflow.com/questions/7390902/requiredif-conditional-validation-attribute?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa. Check this. As such you don't need to create client side validation, however, better to have it. While sending the data, just make sure that dependent property is false so that RequiredIf don't validate the attached property. – user1672994 Jun 01 '18 at 08:11
  • ZerosAndOnes: The error message I'm getting is "Required". The Data Annotation is configured as: [Required(AllowEmptyStrings = false, ErrorMessageResourceName = "Registration_SiblingEnrolledFirstName_RequiredErrorMessage", ErrorMessageResourceType = typeof(Resources.SharedResources))] – user3602841 Jun 01 '18 at 08:12
  • user1672994: okay, that might work. Will try in the morning and post thereafter if it works or not. Thanks. – user3602841 Jun 01 '18 at 08:18
  • user1672994: The 2nd link did not help. All samples are for the IClientValidatable interface, not the IClientModelValidator interface used by .Net Core. I not problem getting the server-side to work, but the client-side doesn't work. Difficult to debug without having to go debug into the jquery.validate.unobstrusive.js code. It looks like the methods are registered, just not executed when field losses focus. – user3602841 Jun 02 '18 at 01:08
  • Would be helpful to see a working sample, but haven't been able to find one. – user3602841 Jun 02 '18 at 01:09

0 Answers0