How to Show a particular field when a dropdown item is changed?


#1

In this example, http://ui.agiletoolkit.org/demos/jscondform.php, when the subscribe is changed the email field appears.

Instead of using a checkbox field, how would you do the same thing using a dropdown?

Sample code:
//The usuals:
require ‘…/atk/vendor/autoload.php’;
$app = new \atk4\ui\App(’ ');
$app->initLayout(‘Centered’);

$f_sub = $app->add(new \atk4\ui\Form([‘segment’]));
$f_sub->add([‘Label’, ‘Select Data type and add to Metric definition’, ‘top attached’], ‘AboveFields’);

$f_sub->addField(‘type’, [‘DropDown’, ‘caption’=>‘Data Type’, ‘values’ => [‘one’, ‘two’, ‘three’]]);
$f_sub->addField(‘email’);

// Show email if dropdown labeled type has a value of ‘two’
$f_sub->setFieldsDisplayRules([
//This does not work, what is the proper line of code here?
‘email’=> [‘type’=> ‘Single’],
]);


#2

Rules are based on Romantic ui Form validation rules
https://fomantic-ui.com/behaviors/form.html

So in your case it should be;

'email' => ['type' => 'isExactly[0]'] // 0 being the index value 'one' in your Dropdown field

you may also use key value in your rule if you set Dropdown value using key.

$f_sub->addField(‘type’, [‘DropDown’, ‘caption’=>‘Data Type’, ‘values’ => ['one' => ‘One’, 'two' => Two’, ‘three’ => 'Three]]);

Then you can setup your rule as follow:

'email' => ['type' => 'isExactly[one]']