Usage: Creating of dynamic field
Product: Custom Elements Contao
Contao Version: 4.9.3
Hi there,
in a project that I'm, currently working I need to create a dynamic field based on the value selected on another field.
I was able to use the options_callback on the first field to get the data from the database.
'options_callback' => function (){
$ships = array();
$results = \Database::getInstance()->prepare("SELECT id, alias FROM tl_ship")->execute()->fetchAllAssoc();
foreach ($results as $result) {
$ships[$result['id']] = $result['alias'];
}
return $ships;
},
But I can't figure it out how to save the data on the other field, is this approach right?
'ship_data' =>array(
'label' => array("ship data","ship data"),
'onsave_callback' => function (){
$ship = \Database::getInstance()->prepare("SELECT alias,title FROM tl_ship WHERE id=?")->execute(\Input::post('rsce_field_custom_ship'))->fetchAllAssoc();
var_dump($ship);
return(json_encode($ship));
}
),
Keyboard shortcuts
Generic
? | Show this help |
---|---|
ESC | Blurs the current field |
Comment Form
r | Focus the comment reply box |
---|---|
^ + ↩ | Submit the comment |
You can use Command ⌘
instead of Control ^
on Mac
Support Staff 1 Posted by RockSolid Theme... on 09 Nov, 2020 02:54 PM
Thank you for submitting your question!
There is no
onsave_callback
for DCA fields, you should use thesave_callback
(withouton…
) instead.You can find more information about the available callbacks in the Contao documentation: https://docs.contao.org/dev/reference/dca/fields/#reference