Skip to content

Commit 576d1b8

Browse files
committed
Active tab will default to the tab with the first error present. Tab icons will also be changed to an option called icon_error to let users know there is an error in that tab.
1 parent 4814ad0 commit 576d1b8

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

Form/Extension/TabbedFormTypeExtension.php

+17-4
Original file line numberDiff line numberDiff line change
@@ -48,23 +48,36 @@ public function finishView(FormView $view, FormInterface $form, array $options)
4848
return;
4949
}
5050

51-
$found_first = false;
51+
$activeTab = null;
52+
$tabIndex = 0;
53+
$foundInvalid = false;
5254
$tabs = array();
5355

5456
foreach($view->children as $child) {
5557
if(in_array('tab', $child->vars['block_prefixes'])) {
56-
if (!$found_first) {
57-
$child->vars['tab_active'] = $found_first = true;
58+
59+
$child->vars['tab_index'] = $tabIndex;
60+
$valid = $child->vars['valid'];
61+
62+
if (($activeTab === null || !$valid) && !$foundInvalid) {
63+
$activeTab = $child;
64+
$foundInvalid = !$valid;
5865
}
5966

60-
$tabs[] = array(
67+
$tabs[$tabIndex] = array(
6168
'id' => $child->vars['id'],
6269
'label' => $child->vars['label'],
6370
'icon' => $child->vars['icon'],
71+
'active' => false,
6472
);
73+
74+
$tabIndex++;
6575
}
6676
}
6777

78+
$activeTab->vars['tab_active'] = true;
79+
$tabs[$activeTab->vars['tab_index']]['active'] = true;
80+
6881
$tabsForm = $this->formFactory->create(new TabsType(), null, array(
6982
'tabs' => $tabs,
7083
'attr' => array(

Form/Type/TabType.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
1313
{
1414
$resolver->setDefaults(array(
1515
'icon' => null,
16+
'error_icon' => 'remove-sign',
1617
));
1718
}
1819

1920
public function buildView(FormView $view, FormInterface $form, array $options)
2021
{
21-
$view->vars['icon'] = $options['icon'];
22+
$view->vars['valid'] = $valid = !$form->isSubmitted() || $form->isValid();
23+
$view->vars['icon'] = $valid ? $options['icon'] : $options['error_icon'];
2224
$view->vars['tab_active'] = false;
23-
25+
2426
$view->parent->vars['tabbed'] = true;
2527
}
2628

Resources/views/Form/fields.html.twig

+2-2
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
{% spaceless %}
8787
<ul class="{{ form.vars.attr.class }}">
8888
{% for tab in form.vars.tabs %}
89-
<li{% if loop.first %} class="active"{% endif %}>
89+
<li{% if tab.active %} class="active"{% endif %}>
9090
<a data-toggle="tab" href="#{{ tab.id }}">
9191
{% if tab.icon %}<i class="icon-{{ tab.icon }}"></i>{% endif %}
9292
{{ tab.label }}
@@ -487,7 +487,7 @@
487487
{% set data_prototype = 'collection' in form.vars.block_prefixes and not omit_collection_item ? '<div class="collection-item ' ~ widget_items_attr.class|default()|join(' ') ~ '" id="' ~ prototype.vars.id ~ '_control_group">' ~ form_row(prototype) ~ '</div>' : form_row(prototype) %}
488488
{% set data_prototype_name = form.vars.form.vars.prototype.vars.name|default('__name__') %}
489489
{% set widget_form_group_attr = widget_form_group_attr|merge({'data-prototype': data_prototype, 'data-prototype-name': data_prototype_name})|merge(attr) %}
490-
{% endif %}
490+
{% endif %}
491491
{% set widget_form_group_attr = widget_form_group_attr|merge({'id': id ~ '_control_group', 'class': widget_form_group_attr.class|default('') ~ ' ' ~ row_wrapper_class}) %}
492492
{% if errors|length > 0 %}
493493
{% set widget_form_group_attr = widget_form_group_attr|merge({'class': widget_form_group_attr.class|default('') ~ ' error'}) %}

0 commit comments

Comments
 (0)