drupal: Form API, dynamically hide or show fields based on input

蹲街弑〆低调 提交于 2019-12-03 06:39:06

You can use the #states property to achieve that. The #states property can be applied to all Form API elements.

Here's the documentation link with an example.

Hope this helps... Muhammad.

simple usage example of #states: To show a select field with name 'item' only if another field with name 'type' has value 'sell'

$form['item'] = array( 
        '#title' => t('Task Item'),
        '#type' => 'select',
        '#states' => array(
            // Only show this field when the value of type is sell.
            'visible' => array(
                ':input[name="type"]' => array('value' => 'sell'),
            ),
        ),
    );

You could also use 'Conditional Fields' module. Here is the link: https://drupal.org/project/conditional_fields It provides a 'Manage Dependencies' tab while creating a content type where you can select which fields to be visible when a field has a particular value.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!