Sidus/EAVModelBundle 

Blazing fast data modeling with Symfony

View on GitHub
Fork me on GitHub

DataType form configuration

This chapter will cover the usage of the attributes_config options in the DataType forms.

The default behavior of the Data form type is to take all the attributes present in the family and display a widget for each one (except the one with the hidden option).

You can completely "escape" this behavior by setting the attributes_config, this allows you to configure each attribute that will be used to generate the form.

You can also use the merge_attributes_config option if you want to keep the default attributes config and just override some of them.

So for each attribute in the family, the configuration reference is the following: (in yml for the example)

attributes_config:
    <attributeCode>:
        # Defaults to the __toString of the attribute, see translation chapter for more information
        label: <string>

        # Defaults to the form_type of the attribute
        form_type: <string>

        # Defaults to the hidden option of the attribute
        hidden: <bool>

        # Defaults to [], will be merged by default with the attribute form_options
        form_options: <array>

        # Defaults to true: if set to false the form_options of the attribute will be ignored
        merge_form_options: <bool>

        # Default to attribute option, careful with this one!
        multiple: <bool>

        # Add new constraints to the generated form, default one will still be used!
        validation_rules: <array>

        # Default form type to use with collections
        collection_type: <string>

This configuration can be used in many ways to override the generation of the forms. Extending this behaviour can be done by extending the AttributeFormBuilder that holds all the logic.

Switching from a data_selector type to an embed type

It's possible to switch from one type to the other with the attributes_config.

Example: data_selector to embed

sidus_eav_model:
    families:
        People:
            attributeAsIdentifier: email
            attributeAsLabel: fullName
            attributes:
                email:
                    type: string_identifier
                    validation_rules:
                        - Email:
                            strict: false
                fullName:
                    type: string
                organization:
                    type: data_selector
                    options:
                        allowed_families:
                            - Organization

Custom configuration:

<?php
/** @var \Symfony\Component\Form\FormFactoryInterface $formFactory */
$form = $formFactory->create(\Sidus\EAVModelBundle\Form\Type\DataType::class, null, [
    'family' => 'People',
    'attributes_config' => [
        'email' => null, // Will use the default configuration if left null
        'fullName' => null,
        'organization' => [
            'form_type' => \Sidus\EAVModelBundle\Form\Type\DataType::class,
            'form_options' => [
                'family' => 'Organization',
            ],
            'validation_rules' => [
                new \Symfony\Component\Validator\Constraints\Valid(),
            ],
        ],
    ],
]);

Let's see what's changed:

  • The form_type is required because it's necessary to change the widget of the form from a selector to an embed form.
  • The form_options needs the family parameters because it's not injected by default.
  • The validation rule is needed because by default the related entity in a simple data_selector is not validated by the parent form.