Use Bootstrap static control from inside form by derpue · Pull Request #290 · braincrafted/bootstrap-bundle

Convenience stuff to define BS static form controls inside the forms creation.

This could of course all be done with fiddling in the template, rendering the form row-wise and using the twig form_control_static() function, but i'm often in the need to display one or more fields from my entity as static controls, and this let me do a simple {{ form(form, {'style': 'horizontal'}) }} in the template.

This patch consists of two commits which are independent from each other, just provide different ways and usages:

Extension

Provides for all fields which are rendered as form_widget_simple(date, time, datetime with 'widget' => 'single_text' set, number, integer, url, email) to be rendered as static control, using :

// simple field:
$builder->add('fieldname', 'number', array('static_control' => true));

// datetimefield with formatting:
$builder->add('updatedAt', 'datetime', array(
                'widget' => 'single_text',
                'static_control' => true,
                'format' => 'dd.MM.yyyy HH:mm:ss',
            ));

You should always set the field type explicitly instead of using autoguessing, which leads sometimes to extra attributes in the markup (eg maxlength) that break html validation.

Static control field type

Just another way to get a 'static control' rendered in the form:

// with a fields value (should be convertable to string):
$builder->add('fieldname', 'bs_static', $optionsIfNeeded);

// really static content
$builder->add('somenamenotafield', 'bs_static', array('mapped' => false, 'data'=> 'YourContentHere'));

// fields as datetime which need a Transformer (note the two `$builder->` calls):
$builder->add(
    $builder->create('updatedAt', 'bs_static', $optionsIdNeeded)
                 ->addViewTransformer(new DateTimeToStringTransformer(null, null, 'd.m.Y H:m:s'))
);

I'm not sure which one is the best to use, as both have advantages. Maybe just take both:)

And sorry for not providing tests, i still need to learn/understand that mockup stuff