Skip to content

Commit

Permalink
Added ability to disable form tabs closes #951
Browse files Browse the repository at this point in the history
  • Loading branch information
isometriks committed Oct 17, 2014
1 parent 850a4ea commit 29c9954
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 1 deletion.
1 change: 1 addition & 0 deletions Form/Extension/TabbedFormTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public function finishView(FormView $view, FormInterface $form, array $options)
'label' => $child->vars['label'],
'icon' => $child->vars['icon'],
'active' => false,
'disabled' => $child->vars['disabled'],
'translation_domain' => $child->vars['translation_domain'],
);

Expand Down
2 changes: 2 additions & 0 deletions Form/Type/TabType.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
$resolver->setDefaults(array(
'icon' => null,
'error_icon' => 'remove-sign',
'disabled' => false,
));
}

Expand All @@ -42,6 +43,7 @@ public function buildView(FormView $view, FormInterface $form, array $options)
$view->vars['valid'] = $valid = !$form->isSubmitted() || $form->isValid();
$view->vars['icon'] = $valid ? $options['icon'] : $options['error_icon'];
$view->vars['tab_active'] = false;
$view->vars['disabled'] = $options['disabled'];

$view->parent->vars['tabbed'] = true;
}
Expand Down
3 changes: 2 additions & 1 deletion Resources/views/Form/fields.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@
{% spaceless %}
<ul class="{{ form.vars.attr.class }}">
{% for tab in form.vars.tabs %}
<li{% if tab.active %} class="active"{% endif %}>
{% set class = (tab.active ? 'active ' : '') ~ (tab.disabled ? 'disabled' : '') %}
<li{% if class|trim is not empty %} class="{{ class }}"{% endif %}>
<a data-toggle="tab" href="#{{ tab.id }}">
{% if tab.icon %}{{ mopa_bootstrap_icon(tab.icon) }}{% endif %}
{{ tab.label|trans({}, tab.translation_domain) }}
Expand Down
69 changes: 69 additions & 0 deletions Test/Form/AbstractFormRenderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Mopa\Bundle\BootstrapBundle\Test\Form;

use Symfony\Component\Form\Test\TypeTestCase;

abstract class AbstractFormRenderTest extends TypeTestCase
{
protected $twig;

public function testForm()
{
$fixture = sprintf('%s/Fixtures/%s', __DIR__, $this->getFixture());

if (!file_exists($fixture)) {
$this->markTestSkipped(sprintf('Could not find fixture: %s', $fixture));

return;
}

$contents = file_get_contents($fixture);

die($this->render($this->getForm()));
}

abstract public function getFixture();
abstract public function getForm();

public function render($form)
{
return $this->getTwig()->render('{{ form(form) }}', array('form' => $form->createView()));
}

public function getTwig()
{
if ($this->twig === null) {

$fileLoader = new \Twig_Loader_Filesystem(array(
__DIR__ . '/../../Resources/views',
));

$loader = new \Twig_Loader_Chain(array(
$fileLoader,
new \Twig_Loader_String(),
));

$this->twig = new \Twig_Environment($loader);
$engine = new \Symfony\Bridge\Twig\Form\TwigRendererEngine(array(
'Form/fields.html.twig',
));

$renderer = new \Symfony\Bridge\Twig\Form\TwigRenderer($engine);

$this->twig->addExtension(new \Symfony\Bridge\Twig\Extension\FormExtension($renderer));
$this->twig->addExtension(new \Mopa\Bundle\BootstrapBundle\Twig\IconExtension('glyphicons'));
$this->twig->addExtension(new \Mopa\Bundle\BootstrapBundle\Twig\FormExtension());
$this->twig->addExtension(new \Symfony\Bridge\Twig\Extension\TranslationExtension($this->getMock('Symfony\Component\Translation\TranslatorInterface')));
}

return $this->twig;
}

public function getExtensions()
{
return array(
new Extension\TestExtension(),
);
}
}
20 changes: 20 additions & 0 deletions Test/Form/Extension/TestExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Mopa\Bundle\BootstrapBundle\Test\Form\Extension;

use Mopa\Bundle\BootstrapBundle\Form\Extension\HorizontalFormTypeExtension;
use Symfony\Component\Form\AbstractExtension;

class TestExtension extends AbstractExtension
{
public function loadTypeExtensions()
{
return array(
new HorizontalFormTypeExtension(array(
'horizontal_label_class' => 'col-sm-3',
'horizontal_label_offset_class' => 'col-sm-offset-3',
'horizontal_input_wrapper_class' => 'col-sm-9',
)),
);
}
}
3 changes: 3 additions & 0 deletions Test/Form/Fixtures/widget_simple.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div class="something">

</div>
24 changes: 24 additions & 0 deletions Test/Form/WidgetSimpleRenderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Mopa\Bundle\BootstrapBundle\Test\Form;

class WidgetSimpleRenderTest extends AbstractFormRenderTest
{
public function getFixture()
{
return 'widget_simple.html';
}

public function getForm()
{
$form = $this->builder->create('form1', 'form', array(
'horizontal' => true,
));

$form
->add('child1', 'text')
;

return $form->getForm();
}
}

0 comments on commit 29c9954

Please sign in to comment.