Custom Elements onsubmit callback

aldorr's Avatar


01 Aug, 2016 06:10 AM

This comment was split from the discussion: Verwendung: Custom Elements und onsubmitCallback (onsubmit_callback)

Dear Rocksolid,

Your custom elements plugin makes things a lot easier on me.
Now I'd like to add a callback function, as well and it seems like it should be pretty simple.
I've added the following to the Custom Element config file:

'onloadCallback' => array(
    function() {
        $GLOBALS['TL_DCA']['tl_content']['config']['onsubmit_callback'][] = array('dateEndCheck', 'function');
I'm assuming that now I need a function called dateEndCheck.
Where would that go?
I'd like to have it fill my CE 'dateend' with the same value as 'datestart', but only if 'dateend' is blank.

Something like:

public function dateEndCheck(DataContainer $dc) {
  if (!$dateend){
    $dateend = $datestart;
Although, I'm sure that's not quite going to do it. I'm having trouble getting enough English-speaking documentation on Contao to figure this out.
And chance I can get a hand?

Thanks so much.

  1. Support Staff 1 Posted by RockSolid Theme... on 01 Aug, 2016 06:10 AM

    RockSolid Themes's Avatar

    Thank you for submitting your question!

    You can use a function instead of the array if you don’t want to create an extra PHP class for it. It could look like this:

    'onloadCallback' => array(
        function() {
            $GLOBALS['TL_DCA']['tl_content']['config']['onsubmit_callback'][] = function($dc) {
                  // Your code...
  2. 2 Posted by Reiner on 05 Oct, 2017 03:25 PM

    Reiner's Avatar

    I want to set a field within my custom element with a value, calculated upon other fields of the custom element with this callback.
    How do I address the fields? $dc->fieldname doesn't work.
    Thanks for your help

  3. Support Staff 3 Posted by RockSolid Theme... on 05 Oct, 2017 04:32 PM

    RockSolid Themes's Avatar

    It should be possible to access the values with \Input::post('rsce_field_FIELDNAME')

  4. 4 Posted by Reiner on 06 Oct, 2017 01:37 PM

    Reiner's Avatar

    sorry, I don't get this to work
    is it possible that a textfield B is predefine with an calculated value as soon as a value is changed in field A?
    if yes, could someone give me a hint how to do it?

  5. 5 Posted by Reiner on 06 Oct, 2017 04:13 PM

    Reiner's Avatar

    OK, success it works now for one field, but I have another CE with a field from the type „list“, is there a way to do,that for all of the listelements?

  6. Support Staff 6 Posted by RockSolid Theme... on 06 Oct, 2017 04:27 PM

    RockSolid Themes's Avatar

    The first field of list elements can be accessed by \Input::post('rsce_field_LISTNAME__0__FIELDNAME'). If you replace 0 with the list index you can access every list element.

  7. 7 Posted by Reiner on 06 Oct, 2017 06:17 PM

    Reiner's Avatar

    how do I know, how many listelements are listet?

  8. Support Staff 8 Posted by RockSolid Theme... on 09 Oct, 2017 07:11 AM

    RockSolid Themes's Avatar

    You can create a loop that iterates as long as a value was sent:

    for ($i = 0; isset($_POST['rsce_field_LISTNAME__'.$i.'__FIELDNAME']); $i++) {
  9. 9 Posted by Reiner on 09 Oct, 2017 11:22 AM

    Reiner's Avatar

    OK, but there is another Problem!

    I can (read) access every field with \Input::post('rsce_field_LISTNAME__'.$i.'__FIELDNAME'); now, but how do I change the value? If I'm in the save_callback function I'm only able to return a value for the actual field, not for all listelements.
    Is there a way to pass the number/index of the listelement to the save_callback?

    the goal is to calculate the following:

    rsce_field_LISTNAME__0__FIELDNAME1' = function (rsce_field_LISTNAME__0__FIELDNAME2)

  10. Support Staff 10 Posted by RockSolid Theme... on 09 Oct, 2017 01:16 PM

    RockSolid Themes's Avatar

    You can access the field name via $dc->field in the save_callback, e.g.:

    'save_callback' => array(function($value, $dc) {
        $index = explode('__', $dc->field)[1];
  11. 11 Posted by Reiner on 09 Oct, 2017 02:10 PM

    Reiner's Avatar

    OK, that's tricky!
    Thanks for your help, it works now.

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


? 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