$(document).ready(function () { /** * Prepares all included fieldsets for $form indexed * by its label (legend). * * @param {type} $form * @returns {$lastFieldSet$fieldSet} Array of fieldsets indexed by its label */ var getPreparedFieldSets = function ($form) { var result = {}; var $lastFieldSet; // Assamble all fieldsets with label $form.find('fieldset').each(function () { var $fieldSet = $(this).hide(); var legend = $fieldSet.children('legend').text(); // If we have a label we add the fieldset as is else we append its inputs to the previous fieldset if (legend && legend.length) { result[legend] = $lastFieldSet = $fieldSet; } else if($lastFieldSet) { $lastFieldSet.append($fieldSet.children(".form-group")); } }); return result; }; /** * Check for errors in a specific category. * @param _object * @returns {boolean} */ var hasErrors = function($fieldSet) { return $fieldSet.find('.error, .has-error').length > 0; }; /** * Initialize tabbed forms. * Note: this currently does only work with on form per page because of the tab id's */ $('[data-ui-tabbed-form]').each(function () { var activeTab = 0; var $form = $(this); var $tabContent = $('
'); var $tabs = $(' '); $form.prepend($tabContent); $form.prepend($tabs); var index = 0; $.each(getPreparedFieldSets($form), function(label, $fieldSet) { // activate this tab if there are any errors if (hasErrors($fieldSet)) { activeTab = index; } // init tab structure $tabs.append('