Checkboxes with Editor

Checkboxes with Editor

kthorngrenkthorngren Posts: 20,269Questions: 26Answers: 4,765

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?

Kevin

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,650Questions: 1Answers: 10,094 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).

    Regards,
    Allan

  • kthorngrenkthorngren Posts: 20,269Questions: 26Answers: 4,765

    Well, that was simple, thanks :smile:

    Kevin

This discussion has been closed.