We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 28120
    • 380 Posts
    How do I set the default value of a checkbox?

    FormItIsChecked works great for keeping the user entered value during validation but I can't see how to set the checkbox to "checked" when the form first loads and use [[!+fi.myfield:FormItIsChecked=`1`]]

    Which begs the question how would I set the initial value of any field?

    Thanks

    Mark
      • 28120
      • 380 Posts
      Wrote my own snippet

      [[!FormItIsCheckedDefault? &errors=`[[!+fi.validation_error]]` ]]


      working on the basis there aren't any errors when the form first loads so [[!+fi.validation_error]] is 0

      $output = 'checked="checked"';
      if ($errors==1) {
          $output = '';
      }
      
      return $output;
        • 3749
        • 24,544 Posts
        There may be an easier way, but I usually do it with a custom snippet higher up on the page. In this example, I store and retrieve the default fieldnames to check from the snippet properties so they'll be sticky and reset that value from the $_POST array when the form is submitted. I had to move some code around for this example to keep the size down, so the overall logic may not be correct, but it should give you the idea.

        In the Tpl chunk:

        <input type="checkbox" name="sitecheck_tests[]" [[!+checkCharsetChecked]] value="checkCharset"/> Check DB Character Set<br/>


        In the snippet:

        if ($formSubmitted) {
            if (isset($_POST['sitecheck_tests']) && is_array($_POST['sitecheck_tests'])) {
                /* this part updates the default properties */
                foreach($_POST['sitecheck_tests'] as $test) {
                    $properties[$test] = true;
                }
        
                $defaults = implode(',', $_POST['sitecheck_tests']);
            } else {
                $defaults = '';
            }
        
            /* set defaults in snippet properties to make them sticky */
           $snippet = $modx->getObject('modSnippet', array('name' => 'SiteCheck'));
           if (!$snippet) {
               die('Could not fine SiteCheck snippet');
           }
           $props = $snippet->getProperties();
           $props['defaultTests'] =  $defaults;
           $snippet->setProperties($props);
           $snippet->save();
        } else {
        
            /* This part sets the checkboxes */
            $defaults = $modx->getOption('defaultTests', $scriptProperties, '');
            $defaults = explode(',',$defaults);
            $chunk =  $modx->getChunk('SiteCheckTpl');
            foreach($defaults as $default) {
                $chunk = str_replace('[[!+' . trim($default) . 'Checked]]', 'checked="checked"', $chunk );
            }
            return $chunk;
        }
        
        


        If you show the form again after submission, the page needs to be uncached.
          Did I help you? Buy me a beer
          Get my Book: MODX:The Official Guide
          MODX info for everyone: http://bobsguides.com/modx.html
          My MODX Extras
          Bob's Guides is now hosted at A2 MODX Hosting