Using a text input field as a selector
Hi all,
Just wondering if it's possible to define what fields are displayed depending on what value exists for a text input.
Ie for the field:
Code:
'ruleType' => array
(
'label' => &$lang['ruleType'],
'exclude' => true,
'filter' => false,
'inputType' => 'select',
'options' => array(array('test','AWFTest')),
'eval' => array('tl_class'=>'w50', 'readonly'=>true, 'doNotSave'=>true, 'disabled'=>'disabled'),
'load_callback' => array(
array('tl_AWQueue','ruleTypeValue')
)
),
Re: Using a text input field as a selector
To control the available options you need to use the options_callback (search for it in the contao code to find lots of examples). You can probably find the input value to determine the options with.
Re: Using a text input field as a selector
Do you mean changing the palette depending on the selection made in a select input?
For this you could also put a submitOnChange in the ruleType fields eval, array and use the options as subpalettes to load certain other fields -
or you could use an onload callback to change the palette dependant on if ($dc->activeRecord->ruleType == 'test') - also triggered by submitOnChange.
Theres many ways to skin a cat, thats what makes Contao so great.
Re: Using a text input field as a selector
It would help if I put the original code in the post, not the experiment stuff that didn't work. The previous code was me trying to sort the issue using a select.
Anyway this was the original code:
Code:
'ruleType' => array
(
'label' => &$lang['ruleType'],
'exclude' => true,
'filter' => false,
'inputType' => 'text',
'eval' => array('tl_class'=>'w50', 'readonly'=>true, 'doNotSave'=>true, 'disabled'=>'disabled'),
'load_callback' => array(
array('tl_AWQueue','ruleTypeValue')
)
),
So that is, depending what value is set on the field via load_callback changes the palette structure. For extra reference this is what ruleTypeValue contains:
Code:
public function ruleTypeValue($value, $dc)
{
$objRule = $this->Database->prepare('SELECT `type` FROM `tl_AWRule`, `tl_AWQueue` WHERE `tl_AWQueue`.`rule` = `tl_AWRule`.`id` AND `tl_AWQueue`.`id`=?')
->execute($dc->id);
return $objRule->type;
}
So it picks the string value from another table depending on a relationship with the one being referenced in the DCA.
I would guess that this approach won't work due to the palettes being finalised when load_callback has been called and might require onload_callback to be used for the table.
EDIT:
My suspicions were correct by doing it this way:
For config/onload_callback:
Code:
public function populateSubpalettes($dc)
{
$objRule = $this->Database->prepare('SELECT `type` FROM `tl_AWRule`, `tl_AWQueue` WHERE `tl_AWQueue`.`rule` = `tl_AWRule`.`id` AND `tl_AWQueue`.`id`=?')
->execute($dc->id);
if(array_key_exists('ruleType'.$objRule->type,$GLOBALS['TL_DCA']['tl_AWQueue']['subpalettes']))
{
foreach($GLOBALS['TL_DCA']['tl_AWQueue']['palettes'] as $palette=>$fields)
{
$GLOBALS['TL_DCA']['tl_AWQueue']['palettes'][$palette] =
str_replace('ruleType','ruleType,'.$GLOBALS['TL_DCA']['tl_AWQueue']['subpalettes']['ruleType'.$objRule->type],
$GLOBALS['TL_DCA']['tl_AWQueue']['palettes'][$palette]);
}
}
}
Which for any subpalettes it will pick up just like accordian in tl_content:
Code:
'subpalettes' => array
(
'ruleTypeAWFTest' => 'field1'
),
Any better suggestions are certainly welcome.
Re: Using a text input field as a selector
Without thinking about code, which is a better useability option for ruleType - being a textfield or a select dropdown?
Re: Using a text input field as a selector
ruleType is a read only field for display purposes only, the idea is to display what type of rule has been set in the record and provide the other form fields related to it.
Re: Using a text input field as a selector
Depending:
If the value of ruleType is set in the db (by the save, or saved by another function or saved with a save_callback)
then onload wouldn't need the database call ($dc->activeRecord->ruleType would hold its value).
Nevertheless, however its done you've got the right principal
onload to string replace the palette.
If it works, it works :D !