PHPackages                             philkingston/form-bundle - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. philkingston/form-bundle

ActiveSymfony-bundle

philkingston/form-bundle
========================

Provides Twitter Bootstrap form theme, a help type extension, Ajax/Tunnel/Select2 entity form types and javascript helpers

v1.6.0(8y ago)0252MITPHP

Since Feb 13Pushed 8y ago1 watchersCompare

[ Source](https://github.com/philkingston/ZenstruckFormBundle)[ Packagist](https://packagist.org/packages/philkingston/form-bundle)[ Docs](http://zenstruck.com/project/ZenstruckFormBundle)[ RSS](/packages/philkingston-form-bundle/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependencies (5)Versions (14)Used By (0)

ZenstruckFormBundle
===================

[](#zenstruckformbundle)

Provides Twitter Bootstrap form theme, useful FormType Extensions and javascript helpers

[View Demo](http://sandbox.zenstruck.com/articles/edit/random)

Installation
------------

[](#installation)

1. Add to your `composer.json`:

    ```
    composer require zenstruck/form-bundle:~1.4

    ```
2. *Optional* If using the `ajax_entity_controller` feature, add `zendframework/zend-crypt` to your `composer.json`:

    ```
    composer require zendframework/zend-crypt:~2.0,!=2.1.1

    ```

    **Note:** Version 2.1.1 of `zend-crypt` does not have it's autoloader configured correctly.
3. *Optional* If using the Grouped form feature, add [cocur/slugify](https://github.com/cocur/slugify#symfony2) to your `composer.json`

    ```
    composer require cocur/slugify:~0.8

    ```
4. Register the bundle with Symfony2:

    ```
    // app/AppKernel.php

    public function registerBundles()
    {
        $bundles = array(
            // ...
            new Zenstruck\Bundle\FormBundle\ZenstruckFormBundle(),

            // enable if you want to use the grouped form
            // new Cocur\Slugify\Bridge\Symfony\CocurSlugifyBundle()
        );
        // ...
    }
    ```
5. If using 'Select2', be sure to download the required files from  and include the files in your template.

    ```
    //base.html.twig Example

    //...
    {% block stylesheets %}

    //...
    {% block javascripts %}

    ```

Twitter Bootstrap form layout
-----------------------------

[](#twitter-bootstrap-form-layout)

To use, do one of the following:

- Add for a single template:

    ```
    {# for bootstrap 2.x #}
    {% form_theme form 'ZenstruckFormBundle:Twitter:form_bootstrap_layout.html.twig' %}

    {# for bootstrap 3.x #}
    {% form_theme form 'ZenstruckFormBundle:Twitter:form_bootstrap3_layout.html.twig' %}
    ```
- Add globally in your `config.yml`:

    ```
    twig:
        form:
            resources:
                # for bootstrap 2.x
                - 'ZenstruckFormBundle:Twitter:form_bootstrap_layout.html.twig'

                # for bootstrap 3.x
                - 'ZenstruckFormBundle:Twitter:form_bootstrap3_layout.html.twig'
    ```

FormType Extensions
-------------------

[](#formtype-extensions)

### AjaxEntityType

[](#ajaxentitytype)

[![AjaxEntityType screenshot](https://camo.githubusercontent.com/ed9c751edc388a956a0055bda9831b588a0b471233d9bbc5cd4cabbb3c6d10ac/68747470733a2f2f6c68332e676f6f676c6575736572636f6e74656e742e636f6d2f2d7148355f71333479726a632f5552764245615f657964492f41414141414141414b45592f597977627a3741324f71412f733338342f616a61782d656e746974792e6a7067)](https://camo.githubusercontent.com/ed9c751edc388a956a0055bda9831b588a0b471233d9bbc5cd4cabbb3c6d10ac/68747470733a2f2f6c68332e676f6f676c6575736572636f6e74656e742e636f6d2f2d7148355f71333479726a632f5552764245615f657964492f41414141414141414b45592f597977627a3741324f71412f733338342f616a61782d656e746974792e6a7067)

Creates a `1-m` or `m-m` entity association field. This type simply creates a hidden field that takes an either 1 or multiple comma separated entity ids. **Note:** Ensure the entity has `__toString()` defined.

Enable in your `config.yml` (disabled by default):

```
zenstruck_form:
    form_types:
        ajax_entity: true
```

There are several ways to use this type:

1. Default - creates a hidden field type. It is up to the user to add functionality.

    ```
    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilderInterface;

    class MyFormType extends AbstractType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder
                ->add('name', 'zenstruck_ajax_entity', array(
                    'class' => 'AppBundle:MyEntity' // ensure MyEntity::__toString() is defined
                ))
            ;
        }

        // ...
    }
    ```
2. Select2 with built in entity finder (`zendframework/zend-crypt` required):

    Enable the controller in your `config.yml` (disabled by default):

    ```
    zenstruck_form:
        form_types:
            ajax_entity_controller: true
    ```

    Add the route to your `routing.yml`:

    ```
    zenstruck_form:
        resource: "@ZenstruckFormBundle/Resources/config/ajax_entity_routing.xml"
    ```

    Add to your form type:

    ```
    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilderInterface;

    class MyFormType extends AbstractType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder
                ->add('name', 'zenstruck_ajax_entity', array(
                    'class'             => 'AppBundle:MyEntity', // ensure MyEntity::__toString() is defined
                    'use_controller'    => true,
                    'property'          => 'name', // the entity property to search by
                    // 'repo_method'    => 'findActive' // for using a custom repository method
                    // 'extra_data'     => array() // for adding extra data in the ajax request (only applicable when using repo_method)
                ))
            ;
        }

        // ...
    }
    ```

    **Note:** The URL is dynamically generated for each entity but is encrypted with the application's `secret` for security purposes.
3. Select2 with custom URL. This will create a Select2 widget for this field.

    ```
    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilderInterface;

    class MyFormType extends AbstractType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder
                ->add('name', 'zenstruck_ajax_entity', array(
                    'class' => 'AppBundle:MyEntity', // ensure MyEntity::__toString() is defined
                    'url' => '/myentity/find'
                ))
            ;
        }

        // ...
    }
    ```

    The url endpoint receives the search string as a `q` request parameter and must return a json encoded array. Here is an example:

    ```
    [
        {"id":2004,"text":"dolorem"},
        {"id":2008,"text":"inventore"}
    ]
    ```

#### FormType options

[](#formtype-options)

- `class`: The entity the field represents. *Required.*
- `url`: The url that Select2 will send search queries to
- `property`: The entity property to search by (Overrides `url`)
- `method`: The custom repository method to call for searches (Overrides `property`)
- `placeholder`: The Select2 placeholder text. Default: *Choose an option*
- `multiple`: Whether this is allows for multiple values. Default: *false*
- `use_controller`: Whether to use the bundled controller or not (``). Default: *false*
- `repo_method`: For using a custom repository method. Default: *null*
- `extra_data`: For adding extra data in the ajax request (only applicable when using repo\_method). Default *array()*

#### Select2 Javascript Helper

[](#select2-javascript-helper)

Enables the [Select2](http://ivaynberg.github.com/select2/) widget for `AjaxEntityType`. Requires [Select2](https://github.com/ivaynberg/select2/tags).

Enable with `ZenstruckFormHelper.initSelect2Helper()`

### TunnelEntityType

[](#tunnelentitytype)

[![TunnelEntityType screenshot](https://camo.githubusercontent.com/8eb11cd8834a6180dc1f799ca16855ea143530122c75c34000c0dafba84e2423/68747470733a2f2f6c68332e676f6f676c6575736572636f6e74656e742e636f6d2f2d473454746152496e414e4d2f55527642456a62353431492f41414141414141414b45632f74504f6c453437596a5f732f733432332f656e746974792d74756e6e656c2e6a7067)](https://camo.githubusercontent.com/8eb11cd8834a6180dc1f799ca16855ea143530122c75c34000c0dafba84e2423/68747470733a2f2f6c68332e676f6f676c6575736572636f6e74656e742e636f6d2f2d473454746152496e414e4d2f55527642456a62353431492f41414141414141414b45632f74504f6c453437596a5f732f733432332f656e746974792d74756e6e656c2e6a7067)

Creates an entity association field with a select button. A javascript callback for the select button may be defined. Can be used for opening a dialog to choose an entity.

1. Enable in your `config.yml` (disabled by default):

    ```
    zenstruck_form:
        form_types:
            tunnel_entity: true
    ```
2. Add help option to your form fields

    ```
    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilderInterface;

    class MyFormType extends AbstractType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder
                ->add('name', 'zenstruck_tunnel_entity', array(
                    'class' => 'AppBundle:MyEntity',
                    'callback' => 'MyApp.selectMyEntity',
                    'required' => false
                ))
            ;
        }

        // ...
    }
    ```

The widget html generated by the above example is as follows:

```

    {{ title }}

    Select...

```

Your javascript can hook into the clear button and select button. Here are the useful classes:

- `.zenstruck-tunnel-id`: id of the selected entity
- `.zenstruck-tunnel-title`: title of the selected entity
- `.zenstruck-tunnel-clear`: button that clears the title/id (only available if `required` is `false`)
- `.zenstruck-tunnel-select`: button that initiates the entity selection

#### FormType options

[](#formtype-options-1)

- `class`: The entity the field represents. *Required.*
- `callback`: The javascript callback
- `button_text`: The text for the select button. Default: *Select...*

#### Tunnel Javascript Helper

[](#tunnel-javascript-helper)

Adds events to the clear and select buttons. The select button calls the `callback` defined in the type options. The callback receives the following parameters:

- `id`: the id of the currently selected entity (if any)
- `element`: the hidden input element

Enable with `ZenstruckFormHelper.initTunnelHelper()`

### HelpType

[](#helptype)

Allow you to add help messages to your form fields.

1. Enable in your `config.yml` (disabled by default):

    ```
    zenstruck_form:
        form_types:
            help: true
    ```
2. Add help option to your form fields

    ```
    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilderInterface;

    class MyFormType extends AbstractType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder
                ->add('name', 'text', array(
                    'help' => 'Your full name'
                ))
            ;
        }

        // ...
    }
    ```

### Group Type

[](#group-type)

[![Group Type screenshot](https://camo.githubusercontent.com/ecb9cd0568d2faf9d061b79e76697ce6fcbfcf7a8494b9daa9d01b820fff6d99/68747470733a2f2f6c68362e676f6f676c6575736572636f6e74656e742e636f6d2f2d4c70466b56676737316e632f555762524d46476e3863492f41414141414141414b46632f5842746c4f346234556f6b2f733433332f666f726d2d67726f75702e6a7067)](https://camo.githubusercontent.com/ecb9cd0568d2faf9d061b79e76697ce6fcbfcf7a8494b9daa9d01b820fff6d99/68747470733a2f2f6c68362e676f6f676c6575736572636f6e74656e742e636f6d2f2d4c70466b56676737316e632f555762524d46476e3863492f41414141414141414b46632f5842746c4f346234556f6b2f733433332f666f726d2d67726f75702e6a7067)

This type allows you group large forms into tabs.

1. Enable in your `config.yml` (disabled by default):

    ```
    zenstruck_form:
        form_types:
            group: true
    ```
2. Add help option to your form fields

    ```
    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilderInterface;

    class MyFormType extends AbstractType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder
                ->add('name', 'text', array(
                    'group' => 'Foo'
                ))
                ->add('name', 'text', array(
                    'group' => 'Bar'
                ))
            ;
        }

        // ...
    }
    ```

    **Note:** fields without a group will be in the first, default tab.
3. When creating your form view in your controller, wrap it with `Zenstruck\Bundle\FormBundle\Form\GroupedFormView`

    ```
    class MyController extends Controller
    {
        public function newAction(Request $request)
        {
            // ...
            return array(
                'grouped_form' => new \Zenstruck\Bundle\FormBundle\Form\GroupedFormView($form->createView())
            );
        }
    }
    ```

    **Note:** to name your default tab to something other than *Default*, pass it as the second parameter to the `GroupedFormView` constructor.
4. In your template, include `grouped_form.html.twig` to render the form.

    ```

        {% include 'ZenstruckFormBundle:Twitter:grouped_form.html.twig' with { 'grouped_form': grouped_form } %}

    ```

    **Note:** to use the wrapped form, use `grouped_form.form`

#### Add custom data to `GroupedFormView`

[](#add-custom-data-to-groupedformview)

```
// ..
$groupedForm = new \Zenstruck\Bundle\FormBundle\Form\GroupedFormView($form->createView());
$groupedForm->setData('foo', 'bar');
```

In your template:

```
{# ... #}
{{ grouped_form.data('foo') }} {# returns bar #}
```

#### Custom group order

[](#custom-group-order)

```
$groupedForm = new GroupedFormView($form->createView(), 'Default', array(
    'Bar', 'Foo', 'Default'
));
```

### Theme Type

[](#theme-type)

Allow you to add theme options to your form fields. The `theme_options` variable will be available in your form theme. *The bootstrap3 theme currently utilizes.*

1. Enable in your `config.yml` (disabled by default):

    ```
    zenstruck_form:
        form_types:
            theme: true
    ```
2. Set default theme options in your `config.yml`

    ```
    zenstruck_form:
        theme_options:
            control_width: col-md-4
            label_width: col-md-2
            # ...
    ```
3. Set theme options on a field in your form

    ```
    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilderInterface;

    class MyFormType extends AbstractType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder
                ->add('name', 'text', array(
                    'theme_options' => array('control_width' => 'col-md-6')
                ))
            ;
        }

        // ...
    }
    ```

Miscellaneous Javascript helpers
--------------------------------

[](#miscellaneous-javascript-helpers)

This bundle comes with a set of useful javascript helpers. To enable, add the following javascipt file (or add to your assetic javascripts):

```

```

Initialize all helpers with:

```
$(function() {
    ZenstruckFormHelper.initialize();
});
```

### PostLinkHelper

[](#postlinkhelper)

Allows a standard `` tag to become a method="POST" link. Add the class `method-post`, `method-post-confirm`or `method-delete` to an `` tag for it's href value to become a POST link.

- `method-post`: standard post link (no confirmation)
- `method-post-confirm`: `method-post` with a confirmation dialog that is customizable via the `data-message` attribute
- `method-delete`: cross browser compatible DELETE link with a "Are you sure you want to delete?" confirmation dialog

Enable with `ZenstruckFormHelper.initPostLinkHelper()`

### FormCollectionHelper

[](#formcollectionhelper)

Adds Symfony2 form collection 'add' and 'delete' button functionality. See the [Symfony2 docs](http://symfony.com/doc/current/cookbook/form/form_collections.html). This works out of the box when using the `form_bootstrap_layout.html.twig` form layout provided by this bundle.

**Note:** Do not add the javascript provided in the [Symfony2 cookbook article](http://symfony.com/doc/current/cookbook/form/form_collections.html)

Enable with `ZenstruckFormHelper.initFormCollectionHelper()`

Full default config
-------------------

[](#full-default-config)

```
zenstruck_form:
    form_types:
        help:                   false
        group:                  false
        tunnel_entity:          false
        ajax_entity:            false
        ajax_entity_controller: false
```

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity70

Established project with proven stability

 Bus Factor1

Top contributor holds 80.2% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~154 days

Recently: every ~350 days

Total

12

Last Release

3134d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/9ccda81f2fe32406599a1bfc014fde2f60cbc54b846e6788c2c32e20b5825bc7?d=identicon)[philkingston](/maintainers/philkingston)

---

Top Contributors

[![kbond](https://avatars.githubusercontent.com/u/127811?v=4)](https://github.com/kbond "kbond (81 commits)")[![maxailloud](https://avatars.githubusercontent.com/u/792787?v=4)](https://github.com/maxailloud "maxailloud (10 commits)")[![DougHayward](https://avatars.githubusercontent.com/u/4302775?v=4)](https://github.com/DougHayward "DougHayward (7 commits)")[![ngodfraind](https://avatars.githubusercontent.com/u/1397430?v=4)](https://github.com/ngodfraind "ngodfraind (2 commits)")[![rgb-](https://avatars.githubusercontent.com/u/902990?v=4)](https://github.com/rgb- "rgb- (1 commits)")

---

Tags

form

### Embed Badge

![Health badge](/badges/philkingston-form-bundle/health.svg)

```
[![Health](https://phpackages.com/badges/philkingston-form-bundle/health.svg)](https://phpackages.com/packages/philkingston-form-bundle)
```

###  Alternatives

[simplethings/form-extra-bundle

This bundles provides extra FormType's for Symfony2

10283.6k](/packages/simplethings-form-extra-bundle)[nurikabe/star-rating-bundle

Symfony form type and Twig extension for quick integration of FyneWorks' Star Rating widget

113.1k](/packages/nurikabe-star-rating-bundle)[damianociarla/select-city-form-field-bundle

This Bundle provides a new form field that renders three select 'Country, Region, City' for Symfony2

101.9k](/packages/damianociarla-select-city-form-field-bundle)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
