Skip to content

Latest commit

 

History

History
148 lines (124 loc) · 6.43 KB

field-configuration.md

File metadata and controls

148 lines (124 loc) · 6.43 KB

Field configuration

Because all fields are extended from Yiisoft\Widget\Widget from Yii Widget package, all widget configuration ways are suitable for form fields as well. They are described in Configuring the widget guide section.

Besides that, there are some additional configuration options that are specific to fields.

Theme

This package defines Yiisoft\Form\Theme\Theme as a following set of configuration:

All settings are optional.

Theme container

Theme container is used to register themes. Call initialize() method before using any field with $configs argument, where:

  • key is a theme name;
  • value is a mapping (associative array) between settings' names and their corresponding values.
use Yiisoft\Form\Theme\ThemeContainer;

ThemeContainer::initialize(
    configs: [
        'main' => [ 
            'containerClass' => 'field-container-main',
            // ...            
            'fieldConfigs' => [
                Checkbox::class => [
                    'inputContainerTag()' => ['div'],
                    // ...
                ],
                // ...
            ],
        ],
        'alternative' => [
            'containerClass' => 'field-container-alt',
            // ...            
            'fieldConfigs' => [
                Checkbox::class => [
                    'inputContainerTag()' => ['span'],
                    // ...
                ],
                // ...
            ],
        ],      
    ],
    defaultConfig: 'main',
);

You can additionally set (optional) config used as a default one using defaultConfig option.

Built-in themes

These themes are available out of the box:

  • Bootstrap 5 Horizontal;
  • Bootstrap 5 Vertical.

Their settings are stored in separate configuration files. To simplify including it, you can use Yiisoft\Form\Theme\ThemePath - constants with for all built-in themes.

use Yiisoft\Form\Theme\ThemeContainer;
use Yiisoft\Form\Theme\ThemePath;

ThemeContainer::initialize(
    config: [
        'vertical' => require ThemePath::BOOTSTRAP5_VERTICAL,
        'horizontal' => require ThemePath::BOOTSTRAP5_HORIZONTAL,
    ],
    defaultConfig: 'vertical',
);

In built-in themes no validation rules enrichers are used.

Using with Yii Config

When using Yii Config, there is no need to manually interact with theme cotnainer, it's initialized automatically during application's bootstrap. Here is an example of relevant config/params.php file section for configuration:

use Yiisoft\Form\Theme\ThemePath;

return [
    // ...
    'yiisoft/form' => [
        'themes' => [
            'vertical' => require ThemePath::BOOTSTRAP5_VERTICAL,
            'horizontal' => require ThemePath::BOOTSTRAP5_HORIZONTAL,
        ],
        'defaultTheme' => 'vertical',
    ],
    // ...
];

No built-in themes are used by default.