Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions Form/Extension/StaticControlExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Braincrafted\Bundle\BootstrapBundle\Form\Extension;

use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormBuilderInterface;

/**
* StaticControlExtension
*
* @package BraincraftedBootstrapBundle
* @subpackage Form
* @author André Püschel <[email protected]>
* @copyright 2014 André Püschel
* @license http://opensource.org/licenses/MIT The MIT License
* @link http://bootstrap.braincrafted.com Bootstrap for Symfony2
*/
class StaticControlExtension extends AbstractTypeExtension
{
/**
* {@inheritDoc}
*/
public function buildView(FormView $view, FormInterface $form, array $options)
{
$view->vars['static_control'] = $form->getConfig()->getOption('static_control', false);
}

/**
* {@inheritdoc}
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setOptional(array('static_control'));
}

/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
/* We need to set it to disabled, so Symfony ignores the fact that there is no
data submitted back for this field (mapping=>false is only two way, so not usable) */
if (isset($options['static_control']) && $options['static_control'])
$builder->setDisabled(true);
}

/**
* {@inheritdoc}
* Although we only support a field that provides a somewhat text-value we extend the form field.
* (to be more precise: all fields which will be rendered as form_widget_simple)
* If not we would have to create for every of the text-based types an own extension class.
* This way we also support new text-based types out of the box.
*/
public function getExtendedType()
{
return 'form';
}
}
43 changes: 43 additions & 0 deletions Form/Type/FormStaticControlType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/**
* This file is part of BraincraftedBootstrapBundle.
*/

namespace Braincrafted\Bundle\BootstrapBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

/**
* FormStaticControlType
*
* @package BraincraftedBootstrapBundle
* @subpackage Form
* @author André Püschel <[email protected]>
* @copyright 2014 André Püschel
* @license http://opensource.org/licenses/MIT The MIT License
* @link http://bootstrap.braincrafted.com Bootstrap for Symfony2
*/
class FormStaticControlType extends AbstractType
{

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
#'mapped' => false,
'required' => false,
'disabled' => true,
));
}

public function getParent()
{
return 'text';
}

public function getName()
{
return 'bs_static';
}

}
10 changes: 10 additions & 0 deletions Resources/config/services/form.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
<parameter key="braincrafted_bootstrap.form.type.collection.class">Braincrafted\Bundle\BootstrapBundle\Form\Type\BootstrapCollectionType</parameter>
<parameter key="braincrafted_bootstrap.form.type.money.class">Braincrafted\Bundle\BootstrapBundle\Form\Type\MoneyType</parameter>
<parameter key="braincrafted_bootstrap.form.type.form_actions.class">Braincrafted\Bundle\BootstrapBundle\Form\Type\FormActionsType</parameter>
<parameter key="braincrafted_bootstrap.form.type.form_static_control.class">Braincrafted\Bundle\BootstrapBundle\Form\Type\FormStaticControlType</parameter>
<parameter key="braincrafted_bootstrap.form.extension.typesetter_extension.class">Braincrafted\Bundle\BootstrapBundle\Form\Extension\TypeSetterExtension</parameter>
<parameter key="braincrafted_bootstrap.form.extension.static_control.class">Braincrafted\Bundle\BootstrapBundle\Form\Extension\StaticControlExtension</parameter>
<parameter key="braincrafted_bootstrap.form.extension.input_group_button.class">Braincrafted\Bundle\BootstrapBundle\Form\Extension\InputGroupButtonExtension</parameter>
</parameters>

Expand All @@ -25,10 +27,18 @@
<tag name="form.type" alias="form_actions" />
</service>

<service id="braincrafted_bootstrap.form.type.form_control_static" class="%braincrafted_bootstrap.form.type.form_static_control.class%">
<tag name="form.type" alias="bs_static" />
</service>

<service id="braincrafted_bootstrap.form.extension.typesetter_extension" class="%braincrafted_bootstrap.form.extension.typesetter_extension.class%">
<tag name="form.type_extension" alias="form" />
</service>

<service id="braincrafted_bootstrap.form.extension.static_control" class="%braincrafted_bootstrap.form.extension.static_control.class%">
<tag name="form.type_extension" alias="form" />
</service>

<service id="braincrafted_bootstrap.form.extension.input_group_button" class="%braincrafted_bootstrap.form.extension.input_group_button.class%">
<tag name="form.type_extension" alias="text" />
</service>
Expand Down
15 changes: 12 additions & 3 deletions Resources/views/Form/bootstrap.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
{% endif %}

{% set type = type|default('text') %}
{% set attr = attr|merge({ 'class': (attr.class|default('') ~ ' form-control')|trim }) %}

{% if style == 'inline' and (attr.placeholder is not defined or attr.placeholder is empty) and label != false %}
{% if label is empty %}
Expand All @@ -45,8 +44,13 @@
{% endif %}
{% endif %}

<input type="{{ type }}" {{ block('widget_attributes') }} {% if value is not empty %}value="{{ value }}" {% endif %}>

{% if static_control is defined and static_control == true %}
{% set attr = attr|merge({ 'class': (attr.class|default('') ~ ' form-control-static')|trim }) %}
<p id="{{ id }}" {%- for attrname, attrvalue in attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>{{ value }}</p>
{%- else -%}
{% set attr = attr|merge({ 'class': (attr.class|default('') ~ ' form-control')|trim }) %}
<input type="{{ type }}" {{ block('widget_attributes') }} {% if value is not empty %}value="{{ value }}" {% endif %}>
{%- endif %}
{% if simple_col is defined %}
</div>
{% endif %}
Expand Down Expand Up @@ -530,6 +534,11 @@
{% endfor %}
{% endblock %}

{% block bs_static_widget %}
{% set attr = attr|merge({ 'class': (attr.class|default('') ~ ' form-control-static')|trim }) %}
<p id="{{ id }}" {%- for attrname, attrvalue in attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>{{ value }}</p>
{% endblock %}

{# Labels #}

{% block form_label %}
Expand Down