TCA: visibility of fields dependant on select value

The problem: depending on the chosen value of a select ‘Type’, certain fields should be hidden or visible. If the selected value is “Type A” or “Type B”, the field “val” should be visible, if the selected value is “Type C”, the field “details” should be visible. The solution is surprisingly easy: first step is to define a reload condition in ext_tables.php, add the following to the $TCA of the extension:

$TCA['tx_extensionname_domain_model_modelname'] = array(
	'ctrl' => array(
                ... 
		'requestUpdate' => 'type'
                ...
	),
);

This forces a page reload, as soon as the “type” value is changed. Now add the 3 fields in Configuration/TCA/Modelname.php:

'type' => array(
    'exclude' => 0,
    'label' => 'LLL:EXT:extensionname/Resources/Private/Language/locallang_db.xml:tx_extensionname_domain_model_modelname.type',
    'config' => array(
        'type' => 'select',
        'items' => array(
            array('Type A', 'typeA'),
            array('Type B', 'typeB'),
            array('Type C', 'typeC')
        )
    ),
),
'val' => array(
    'exclude' => 0,
    'label' => 'LLL:EXT:extensionname/Resources/Private/Language/locallang_db.xml:tx_extensionname_domain_model_modelname.val',
    'config' => array(
        'type' => 'input',
        'size' => 255,
        'eval' => 'trim,required'
    ),
    'displayCond' => array(
        'OR' => array(
            'FIELD:type:=:typeA',
            'FIELD:type:=:typeB',
        )
    )
),
'details' => array(
    'exclude' => 0,
    'label' => 'LLL:EXT:extensionname/Resources/Private/Language/locallang_db.xml:tx_extensionname_domain_model_modelname.details',
    'config' => array(
        'type' => 'input',
        'size' => 30,
        'eval' => 'trim,required'
    ),
    'displayCond' => 'FIELD:type:=:typeC'
),

displayCond defines the conditions, under which the field is visible.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.