We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 32046
    • 75 Posts
    Hi, I’m trying to pre-populate a checkbox from a database . My form template (badgesFormTpl) contains:
       <div>
          <label for="badge">Badge?</label>
          <input type="radio" name="badge[]" value="yes" eform="badge:1" />Yes
          <input type="radio" name="badge[]" value="no" />No
       </div>

    eForm call is like this:
    [!loadFormValuesFromDb!]
    [!eForm? &formid=`badgesForm` &noemail=`true` &tpl=`badgesFormTpl` &protectSubmit=`0` &invalidClass=`formerror` &eFormOnBeforeFormParse=`loadFormValues` !]

    and my snippet for the eFormOnBeforeFormParse event is:
       function loadFormValues( &$fields, &$templates ) {
          global $modx;
             
    
             // get data from db record and add to form fields
             $select = $modx->db->select( '*', 'badge_table', 'id = 1' );
             $row = $modx->db->getRow( $select );
             if ( !empty( $row ))
             {
                // I THINK THIS IS WHERE THE PROBLEM IS....
                $fields['badge'] = $row['badge']
             }
          }
          else
          {
             // The form has beed posted, let's not disturb the data.
          }
          return true; // A value of false will stop the eForm parser
       }
    


    I know I’m doing something wrong in the event snippet, because I don’t know how to add a value (presumably to $fields array) to output the checked="checked" code for the relevant radio button
    Anyone know how to do this? Thanks
      • 32046
      • 75 Posts
      Okay, got this sorted now. The eform snippet "loadFormValues" was fine and the issue was actually with the eform template "badgesFormTpl" - it was missing a colon in the validation rule, so changed it from
      eform="badge:1"

      to
      eform="badge::1"

      and eform seems to handle radio beautifully. smiley