Usage: Creating of dynamic field

iv.hysenbelli's Avatar

iv.hysenbelli

09 Nov, 2020 10:19 AM

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));
                }
            ),
  1. Support Staff 1 Posted by RockSolid Theme... on 09 Nov, 2020 02:54 PM

    RockSolid Themes's Avatar

    Thank you for submitting your question!

    There is no onsave_callback for DCA fields, you should use the save_callback (without on…) instead.

    You can find more information about the available callbacks in the Contao documentation: https://docs.contao.org/dev/reference/dca/fields/#reference

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

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