We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 29059
    • 88 Posts
    hello again smiley

    so, now i'm trying to put in to a registration form a simple checkbox for the user to check to accept or leave unchecked to not accept (one of those terms/conditions/agreement things); and yes, it is required for it to be check for the form to submit successfully.

    i believe i've got everything working (it's a miracle!), but i'm wondering if/is it possible (& if so, then how) to keep the checkbox checked if the user checked it, when the submit doesn't work due to a different validation error.

    i've noticed the page seems to refresh & show the validation error(s), and all the other input fields keep the previously inputted data. so why in such a situation can't the checkbox remain checked?

    :::FYI::: i'm not using "FormIt" - i am using "Login/Register" instead.
    @ https://rtfm.modx.com/extras/revo/login:::FYI::: here is my validation code without everything but the part for the checkbox...
    [[!Register?
    	&validate=`nospam:blank,
    		agree:required:true`
    ]]
    :::FYI::: here is my html for the checkbox...
    <input type="checkbox" name="agree" id="agree" checked="" value="Y" [[!+agree:FormItIsChecked=`Y`]] />
    now, i don't know if that part after the value="Y" that's [[!+agree:FormItIsChecked=`Y`]] is correct or not, but it's the only thing i found that seems to work for getting the requirement & error message to work right. i found it @ https://forums.modx.com/thread/44876/validation-errors-for-checkboxes-not-displaying

    lastly, to be clear, the form is submitting when everything validates ok. i'm just trying to get the checkbox to remain checked, if the user checks it, after the form validates as invalid because of another field. make sense?
    thanks.

    This question has been answered by syberknight. See the first response.

      • 42562
      • 1,145 Posts
      Create a snippet: getTHEMchecks
      <?php
      
        if (isset($_POST[$checkboxname])) {
          return 'checked="checked"';
        }
      In your tpls
      <input type="checkbox" name="coolGuy" [[!getTHEMchecks? &checkboxname=`coolGuy`]] >
      <input type="checkbox" name="doubleDouble" [[!getTHEMchecks? &checkboxname=`doubleDouble`]] >
        TinymceWrapper: Complete back/frontend content solution.
        Harden your MODX site by passwording your three main folders: core, manager, connectors and renaming your assets (thank me later!)
        5 ways to sniff / hack your own sites; even with renamed/hidden folders, burst them all up, to see how secure you are not.
        • 29059
        • 88 Posts
        Quote from: donshakespeare at Feb 15, 2016, 06:25 AM
        Create a snippet: getTHEMchecks...

        that's GREAT @donshakespeare, thank you. but... how do i have 2 modx type code bits inside the same tag?

        i have the [[!+agree:FormItIsChecked=`Y`]] thing in the input tag already. i tried adding yours with it, but can't get anything to work that way; so am i left with having to choose whether to "require" the check or have it persist?
          • 46886
          • 1,154 Posts
          This isn't my area, but can't you require the agreement up in the snippet?
          [[!Register?
              &validate=`nospam:blank,
                  [b]agree:required:true[/b]`
          ]]


          I could be totally off base here. Checkboxes are weird.
            • 3749
            • 24,544 Posts
            I think nuan88's method would work, but another way to go is to just throw in a little JavaScript that keeps the form from being submitted and pops up a message if the agree checkbox is not checked. That's the way it's usually done.

            That shouldn't conflict with the persistence since without the checkbox being checked, the form is never submitted.
              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
            • discuss.answer
              • 29059
              • 88 Posts
              hi guys,

              my apologies for not responding sooner.

              so, here's the deal...

              i've got this form working (finally)! but for a tad bit better UX for it, i'd like to find a way to keep these 3 input types to sustain their options if the user does not validate elsewhere in the form when they hit the submit button. when that happens, the form reloads, and all the text inputs persist correctly, but these 3 reset...
              1. input type Radio - "gender"
              2. input type Select - "country"
              3. input type Checkbox - "agree"

              so, i have the javascript validation that does keep the form from being submitted if something isn't right. but still, if you hit the submit button, it does seem to reload the form if something doesn't validate. not sure how to keep that from happening. and i do know it's the JQuery Validate JS stopping it because i have different error messages show up for that vs the ModX validation error messages. i have that there too just as a fallback.

              in an effort to provide all the relevant info, here's what i'm using & my code...


              ...EDITED OUT THE REST DUE TO FINDING THE ANSWER...

              TURNS OUT, THE JQUERY VALIDATE OPTION TO IGNORE HIDDEN FIELDS FIXED ALL MY WOES.

              so, apparently Materialize totally messes with the "select" & "radio" etc fields & adds hidden input text fields & ul/li code to do all the work. adding the "ignore: []" option tells Jquery Validate to ignore the "hidden" commands & validate those hidden fields. VIOLA!!! smiley

              $().ready(function() {
              	$("#registrationForm").validate({
              		ignore: [], // <--needed for select's hidden input that's doing all the work
              		validClass: 'valid',
              		errorClass: 'invalid',
              		errorElement: 'span',
              		focusInvalid: false,
              ...etc...


              -2¢ [ed. note: syberknight last edited this post 8 years, 1 month ago.]
                • 46886
                • 1,154 Posts
                You are now officially a coder. Yeah you don't know php (yet), but you have done the full circuit of technologies. Congrats!
                  • 29059
                  • 88 Posts
                  heh, thanks ;-)