1

I have an accordion JQuery UI control that shows two different sets of fields. At least one set has to be enabled, and when it is, it needs the validation disabled for the other set. This has been really frustrating to implement dynamically.

Finally I settled on maybe just removing the validation rules right before the submit on the set of fields that are collapsed. (So the user is no required to fill that set). However, even then I can't seem to figure out how to attach an event to the form's submit event that will run before the validation! I was this function to run before the JQuery validation. I'm using MVC3. Here's what some of my code looks like:

<div id="accordion">
            <h3><a href="#">Option Set 1</a></h3>
            <div>
                 [Set of fields here]               
            </div>
            <h3><a href="#">OR Option Set 2</a></h3>
            <div>
                 [Set of fields here]  
            </div>
        </div>

3 Answers 3

1

I'm not familiar with ASP.NET MVC at all, but I am familiar with jQuery (as well as the validate plugin) so here goes.

I think you need to make all the fields within the containing <div> disabled whenever that part of your form is being rendered. It can be as simple as $("selector for div").find(":input").attr("disabled", true); (you can flip them back on using false instead of true)

This should work best for 2 reasons.

  1. The jQuery Validate Plugin will skip any disabled elements
  2. Disabled elements are not sent to the server when the form is posted
Sign up to request clarification or add additional context in comments.

1 Comment

This is one of the first things I tried- I couldn't get it right until I had that selector correct. Thanks!
1

when you toggle the divs just remove the classes needed for validation method and disable the input

1 Comment

Yeah, this was my first show... but I guess I was doing it wrong. With some help from Dominic Barnes I figured it out!
0

Can this help? http://docs.jquery.com/Plugins/Validation/Methods/required#dependency-expression

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.