0

i am in a situation where i have to achieve bit wired conditional validation server side and client side. the story as follows

a) suppose i have one dropdown

b) two checkboxes,

c) one radio button list

d) one checkbox list

e) few textbox and

f) save button

1) my requirement is when user select one data from dropdown and submit form then validation message will be showing for all controls except two checkboxes.

2) when user select one data from dropdown and select one of the checkboxes out of 2 and submit form then validation message will be showing for all controls except checkbox list.

i know the requirement is bit wired but i have to do similar kind of thing. so please give me suggestion like how to solve it in mvc with custom validation.

how to use ValidationAttribute and IClientValidatable interface to achieve this validation at client and server side.

thanks

Mou
  • 15,673
  • 43
  • 156
  • 275
  • [THE COMPLETE GUIDE TO VALIDATION IN ASP.NET MVC 3 - PART 2](http://www.devtrends.co.uk/blog/the-complete-guide-to-validation-in-asp.net-mvc-3-part-2) –  Mar 24 '16 at 08:31
  • I think you are approaching it incorrectly even though it might look like validation it is not.... After the user selects from the first drop down only the first drop down can be validated. You can't validate something that has not happened yet. – stink Mar 24 '16 at 14:24
  • if data has been selected from dropdown then validation will not fire for dropdown. thanks – Mou Mar 24 '16 at 17:17

1 Answers1

1

Assuming that you are using Unobtrusive validation, a different approach can be used. The validation for certain fields can be enabled and disabled on the client/server both.

For server side in the ActionMethod you can do something like this,

' Check if any of the two CheckBoxes are not checked, then
  ModelState.Remove("CheckBox1")
  ModelState.Remove("CheckBox2")

' Else
  ModelState.Remove("CheckBoxList")

For Disabling Client Side Unobtrusive Validation (Kindly refer to https://stackoverflow.com/a/12179373/1361888),

 $(function() {
     var settngs = $.data($('form')[0], 'validator').settings;
     settngs.ignore = ".ignore";
 });

So you can basically enable or disable validation for fields on both client and server side. This is the simple approach.

Community
  • 1
  • 1
Adnan Yaseen
  • 833
  • 1
  • 15
  • 44
  • what is the meaning of this line ` ModelState.Remove("CheckBox1") ?` – Mou Mar 24 '16 at 11:01
  • ModelState.Remove("CheckBox1") will remove the validation from server side for any field that you specify. Every error is added in the ModelState dictionary so by this you are just removing the server side validation for any field you want. The JS code that I have provided will be used to ignore the client side validation for any field you want. – Adnan Yaseen Mar 24 '16 at 11:13
  • at client side i do not want to ignore validation rather i want to validate control based on some condition. – Mou Mar 24 '16 at 11:14
  • Kindly check this. This is the answer on the same page that I have provided earlier for disabling client side validation. You can use if conditions to enable and disable the unobtrusive client side validation any where in the view., http://stackoverflow.com/a/26717175/1361888 – Adnan Yaseen Mar 24 '16 at 11:18
  • my requirement is some time client side validation will not fire for few control based on data selection from dropdown. how to achieve it? – Mou Mar 24 '16 at 11:22