Checkboxes with Editor

Checkboxes with Editor

kthorngrenkthorngren Posts: 10,288Questions: 25Answers: 2,347

It looks like the default behavior of checkboxes with the Editor is to send null for unchecked boxes. Looking at the Always shown checkbox example I see the same behavior. Checking the box the Editor sends 1 but unchecked is null. The PHP script appears to have a setFormatter that sets the null value to 0, not sure though (I don't use PHP).

I tried setting the value to 0 in preSubmit but still the server receives null:

        .on( 'preSubmit', function () {
            console.log('preSubmit', editor.field( 'main.enabled' ).get() == 1 ? 1 : 0);   //displays 0
            editor.field( 'main.enabled' ).set(editor.field( 'main.enabled' ).get() == 1 ? 1 : 0);
            console.log('preSubmit', editor.field( 'main.enabled' ).get());   //displays null value
        } );

If I set the unchecked value to 1, (== 1 ? 1 : 1), then the server receives 1. I use a field type plugin for the BootstrapToggle plugin and with that I can send a 0 for unchecked boxes. Do I need to do the same with checkbox field types? Or is there another option like preSubmit that I can use?


This question has an accepted answers - jump to answer


  • allanallan Posts: 52,525Questions: 1Answers: 8,020 Site admin
    Answer ✓

    Hi Kevin,

    Actually it won't send null - if there are no checkboxes checked, it shouldn't send that parameter to the server at all (undefined in Javascript terms), but not many server-side environments have that concept, which will be why you are seeing it as null.

    What to do is use the unselectedValue option of the checkbox type. Set that to whatever you want when there are no checkboxes selected (0 for example).


  • kthorngrenkthorngren Posts: 10,288Questions: 25Answers: 2,347

    Well, that was simple, thanks :smile:


This discussion has been closed.