I'm using vertical tabs in a form, and I keep all the values with $form_state->setRebuild(true);. In this way, the user can change some of the submitted values.

I would like to keep the selected tab active as well. How can I achieve this?

If there aren't other solutions, I will use JavaScript to manually select the tab after the form is reloaded.


You should be able to see which vertical tab was selected at the time your form was submitted. If not, you can certainly use js and a hidden field to keep track of it. Once you have it submitted your form then you can easily set the #default_tab of your vertical tabs based on the value in your the recently selected tab. Something like this:

$form['information'] = array(
  '#type' => 'vertical_tabs',
  '#default_tab' => isset($form_state['values']['selected_tab'][...) ? $form_state['values']['selected_tab'][... : "some_tab",

Another option would be to use a pure js solution. By storing the currently selected tab in local storage and then selecting that tab when the form loads should work as well. In this case you may see your form "flash" as the selected tab is selected but that may or may not be a problem depending on your form and desired UX

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.