-1

I saw this nice blog that describes adding conditional validation to a asp.net mvc viewmodel. However I'm not sure how the validation error is added/bound to the specific model's property. I would like the error message to be added to my normal ModelState object so I could show the error like so in my view:

ValidationMessageFor(model => model.FirstName)

I think if I had access the the ModelState object from the ValidateMethod(..) then I could just do:

ModelState.AddModelError("FirstName", "First name is required");

And everthing would work smoothly. I'm trying to add conditional Validation.

Has anyone done this before that could provide some insight? I tried the code in the blog but it seems incomplete.

Thanks!

RayLoveless
  • 19,880
  • 21
  • 76
  • 94
  • Using `IValidatableObject` is only ever going to give you server side validation (no client side validation) so I suggest you consider using conditional validation attributes (but you have not indicated in your question what validation your trying to perform) –  Jan 11 '18 at 23:13
  • And in the example your linked to, the `ModelState` error is added automatically. –  Jan 11 '18 at 23:14
  • @StephenMuecke, how is the error linked to the specific propertly? It's not working for me. – RayLoveless Jan 11 '18 at 23:17
  • 1
    You have not shown your code - I'm not psychic :) –  Jan 11 '18 at 23:20
  • @StephenMuecke I used the same code that was used in the blog. The validate() was triggered. but like I said I'm not sure how his generic error message is added to the specific field on the page. – RayLoveless Jan 11 '18 at 23:27
  • It will be displayed in your `@Html.ValidationSummary()` in the view. If you want it to be associated with a specific property then you need to use `yield return new ValidationResult(".....", new[]{ "Property1" });` (but as I noted, this will not give client side validation, only server side) –  Jan 11 '18 at 23:32
  • It sounds as if you want a `[RequiredIf]` attribute (i.e. required based on the value of another property), but can really help further until you show your code, and explain what your trying to validate –  Jan 11 '18 at 23:34
  • @StephenMuecke, Thanks! ValidationResult(".....", new[]{ "Property1" }) worked. That was what the blog was missing. I can't believe I missed that overload. Good point with the [RequireIf]. If you add that as an answer I can accept it and give you credit. Cheers. – RayLoveless Jan 12 '18 at 17:07
  • Cant give an answer because you have not explained what you trying to validate, or what code you have tried :) –  Jan 12 '18 at 20:27
  • ..I give up :). – RayLoveless Jan 12 '18 at 21:11
  • Do you not understand the purpose of this site? It to provide a resource for helping other users to find solutions to their problems. you question helps no one - it does not adequately explain what you trying to do, and does not contain relevant code! –  Jan 12 '18 at 21:13

1 Answers1

1

I needed to use an this overload that was missing from the blog as mentioned by Stephen.:

yield return new ValidationResult(".....", new[]{ "Property1" })
RayLoveless
  • 19,880
  • 21
  • 76
  • 94