In this article, we are going to look at building a multistep form in Drupal 8. For brevity, the form will have only two steps in the shape of two completely separate forms. To persist values across these steps, we will use functionality provided by Drupal’s core for storing temporary and private data across multiple requests.
Drupal form_set_error() function provide validation message based field name. So if you have same field name in same page , after the submission you can see both fields are marked as red color(Error class). It validate or show message for right form but it provide error class for all element which are same.
I found a way to solve it. If you render those form using drupal_get_form then you can add one extra line in top to get rid of this problem.
If you use multiple submit button in Drupal from and add ajax call to submit button then you ajax submit button will execute form default validate and submit function. To prevent it from calling those default function you just need to do two following steps.
1) rename my validation function from "myformname_validate" to "myformname_do_validate". This caused my validate function to not be called during the ajax request.
2) attach the "myformname_do_validate" function to my submit button like so:
Sample code in your .module file for Drupal 7 as follow :-
Multiple step form which consists of multiple steps or pages is useful sometimes. It allows you to create a form workflow and helps to make your web site user-friendly and flexible. For example, you can make different contents on next pages based on user inputs from previous pages.
The key things to make a form multiple pages are setting $form_state[‘rebuild’] and $form_state[‘storage’] variables in a submit function.