0

Here is my scenario i have 3 accordion layout. Each accordion contain 1 form. Like that

<div id="collapseOne">

@using(Html.BeginForm("","",FormMethod.Post,new{id="formOne"}))
{

    @Html.TextBoxFor(m=>m.Name)
    @Html.ValidationMessageFor(m=>m.Name)

    @Html.TextBoxFor(m=>m.City)
    @Html.ValidationMessageFor(m=>m.City)

    <input type="button" value="Next" id="btnOne" />

}

</div>
<div id="collapseTwo">


@using(Html.BeginForm("","",FormMethod.Post,new{id="formTwo"}))
{

    @Html.TextBoxFor(m=>m.Age)
    @Html.ValidationMessageFor(m=>m.Age)

    @Html.TextBoxFor(m=>m.Address)
    @Html.ValidationMessageFor(m=>m.Address)

    <input type="button" value="Next" id="btnTwo" />

}

</div>
<div id="collapseThree">

@using(Html.BeginForm("MyAction","MyController",FormMethod.Post,new{id="formThree"}))
{

     @Html.TextBoxFor(m=>m.Email)
     @Html.ValidationMessageFor(m=>m.Email)

     @Html.TextBoxFor(m=>m.City)
     @Html.ValidationMessageFor(m=>m.Email)

    <input type="submit" value="Finish" id="btnThree" />

}

</div>

Now validation checks doing well when i press Next Next in above accordions. But when someone comes directly to 3rd accordion and try to press finish then it validate only 3rd form. Here is my jquery

function PageEvents(){
  $("#btnThree").click(function(event){
    event.preventDefault();
    if($("#formOne").valid()==false){
      // Open first Accordion
    }
    else if($("#formTwo").valid()==false){
      //Open second Accodion
    }
    else{
      if($("#formThree").valid()){
         $("#formThree").submit();
      }
    }
  });

$("#btnOne").click(function (event) {
    event.preventDefault();
    if ($("#formOne").valid()) {
        //trigger accordion two
        $("collapseTwo").trigger("click");
    }
});
$("#btnTwo").click(function (event) {
    event.preventDefault();
    if ($("#formTwo").valid()) {
        //trigger accordion two
        $("collapseThree").trigger("click");
    }
});

If you want more description i will provide. Please guide me how to validate those forms which are not opened yet and user come directly in 3rd accordion and press finish button...

Thanks

5
  • Are you trying to do a multi-step wizard? Commented Dec 11, 2014 at 8:16
  • what's jquery code on btnOne and btnTwo? Commented Dec 11, 2014 at 8:23
  • Exactly @StephenMuecke but actually it is not wizard but act like wizard. Commented Dec 11, 2014 at 9:42
  • @teovankot i updated my Question please take a look Commented Dec 11, 2014 at 9:45
  • 1
    @azhar_SE_nextbridge. This answer shows an alernative using a single form and validating each section on the fly. I'm not sure how you could make this work. Your posting your back to the same method so if you post the 1st form - it would fail server side validation (e.g. if say the email was required) Commented Dec 11, 2014 at 9:46

1 Answer 1

2

You have a problem with validatin hidden fields, jquery validation by default don't validate hidden fields, you can just change default:

$.validator.setDefaults({
    ignore: []
});

Buy also you can diasble tabs, that shouldn't be allowed here is example how to do it.

But the best way is to use miltistep Wizard like this.

Sign up to request clarification or add additional context in comments.

10 Comments

but in this case when i click on btnThree then it returns all form valid even they are not valid yet..
when whole div is hide then obviously it return true against validation.
now i get your problem, updated the answer with solution
and if some controls are decision based like if dropdown value is yes selected then show Age textbox.. Till then it should be hide by default.By your solution it goes to validate all fields.
yes, you right. then maby it's easier to disable tabs that user can't use by your logic?
|

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.