You are reading the documentation for the 2.x version. Switch to the current version 3.x.

Composite

You can create an input composed of other inputs, and transform values into another.

Use class Berlioz\Form\Type\CompositeType to construct a composite type.

Example

Creation of a SEO composite type. The objective is to get the value of type in JSON format.

use Berlioz\Form\Form;
use Berlioz\Form\Transformer\JsonTransformer;
use Berlioz\Form\Type\CompositeType;
use Berlioz\Form\Type\Text;

$seoType = new CompositeType(
    [
        'required' => false,
        'transformer' => new JsonTransformer(),
    ],
    new Text([
        'name' => 'title',
        'label' => 'Title',
        'required' => false,
        'attributes' => ['maxlength' => 60]
    ]),
    new Text([
        'name' => 'description',
        'label' => 'Description',
        'required' => false,
        'attributes' => ['maxlength' => 160]
    ])
);

$form = new Form('form');
$form->add('seo', $seoType);

Result of form will be:

{
    "title": "Value of title",
    "description": "Value of description"
}

Last updated: Thu, 18 Sep 2025 16:20