We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3177
    • 137 Posts
    Hello,
    I just created a Template Variable with the input type "Checkbox".
    the input option value is "LastNews" and the default value is also "LastNews".
    (I want webmasters to be able to switch on/off the last news section on each page)

    In a snippet I read the TV:
    $tv = $modx->getTemplateVar(’LastNews’, "", $modx->documentIdentifier);
    $x = $tv["value"];

    I would expect that one of the elements in the $tv structure indicated if the
    checkbox is checked or not for this page.
    But, in both instances (checked and unchecked) I get the same structure data back.

    So, the question is: How can I check if a Checkbox Template Variable is checked
    for a certain page (document).

    Thanks in advance for your help.


      • 7231
      • 4,205 Posts
      In MODx check box TVs only get an active value, when they are not active there is no value and when they are active they have the value from the input value.

      I think this is not right:
      $x = $tv["value"];


      You are telling it to return the value of a key (or TV) named named value. Try it like this:
      $x = $tv['LastNews'];
        [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

        Something is happening here, but you don't know what it is.
        Do you, Mr. Jones? - [bob dylan]
        • 3177
        • 137 Posts
        Hello,

        I found what it was:
        The Template Variable has a field called "Input Option Values". In there I store the string "1".
        I leave the Default Value field empty.
        Then in my page I see a checkbox which is unchecked.
        Then, the element ’value’ in the structure has no value.
        If I change the TV in the page to checked, then the element ’value’ get the value of the ’Input Opiotn Values’.

        So far this is all expected bahaviour.

        But, when I give the field "Default Value" in the Template Variable screen also the value "1",
        then thing go wrong:
        - The checkbox in my page is checked. (Expected behaviour)
        - Then I uncheck this an do a save.
        - Then I check the structure and see that it still have the value "1".
        This is wrong.
        But I understand the bug now: Since the unchecked state is indicated
        by giving the ’value’ no value, MODX fills in the Default Value.

        So, do not fill in the Default Value in a Template Variable if your input type
        is a checkbox.
        Is this a correct assumption?

        Regards,

        Bert Catsburg
          • 36592
          • 970 Posts
          I think this has something to do with this BUG report.
          http://svn.modxcms.com/jira/browse/MODX-414
            • 7231
            • 4,205 Posts
            So, do not fill in the Default Value in a Template Variable if your input type
            is a checkbox.
            Yes, that is the correct assumption. Should be a rule about this smiley
              [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

              Something is happening here, but you don't know what it is.
              Do you, Mr. Jones? - [bob dylan]