Maslosoft Framework Logo Maslosoft Framework Documentation
Flexible Application Foundation


Configuration Dictionary

Configuration dictionary helper can be used to create dropdown lists in forms containing values taken from application configuration. It also allows to abstract out getting configuration values.

Creating dictionary

To create configuration based dictionary, override DictConfigBase class.

Newly created class need to implement methods:

  • getConfigPath() which should return dot notation path of application configuration array.

In example part of configuration:

$config = [
    'hosting' => [
        'domains' => [
            'example.com' => 'Main domain',
            'example.org' => 'Backup domain'
        ]
    ]
];

To create dictionary with domains, method should return hosting.domains, ie.:

public function getConfigPath()
{
    return 'hosting.domains';
}
  • getDefault() - should return default value if nothing is choosen
  • getEmpty() - should return label for not selected value, ie. Select one...

Using dictionary

To get all values, call getValues() which will return all values available in configuration. This array will not include value from getDefault(). Returned value will be as-is in configuration, from above example it will return array:

$result = [
    'example.com' => 'Main domain',
    'example.org' => 'Backup domain'
];

It is possible to check whether value passed as a parameter is valid by using isValidValue()method. This will return boolean value if passed $value exists in configuration array. This function will validate against key of configuration array, in above example this will check if value is example.com or example.org

Using in forms

Dictionary can be used along with forms from widgets package.

To create dropdown list with dictionary class, add @FormRenderer annotation for model attribute. First parameter is a class literal indicating which renderer should be used - in this case Dict. Second parameter must be class literal with newly created dictionary.

Example model attribute annotations to use dictionary PageDomain:

/**
 * @Label('Domain')
 * @FormRenderer(Dict, PageDomain)
 * @see PageDomain
 * @see Dict
 * @var string
 */
public $domain = '';

Notice the @see doc blocks - these are used for IDE to keep use statements.