<![CDATA[ How to persist checkbox state if submission fails for other reasons & page refreshes? - My Forums]]> https://forums.modx.com/thread/?thread=99542 <![CDATA[How to persist checkbox state if submission fails for other reasons & page refreshes?]]> https://forums.modx.com/thread/99542/how-to-persist-checkbox-state-if-submission-fails-for-other-reasons-page-refreshes#dis-post-538296

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.]]>
syberknight Feb 14, 2016, 05:41 PM https://forums.modx.com/thread/99542/how-to-persist-checkbox-state-if-submission-fails-for-other-reasons-page-refreshes#dis-post-538296
<![CDATA[Re: How to persist checkbox state if submission fails for other reasons & page refreshes?]]> https://forums.modx.com/thread/99542/how-to-persist-checkbox-state-if-submission-fails-for-other-reasons-page-refreshes#dis-post-538775 syberknight Feb 25, 2016, 11:31 AM https://forums.modx.com/thread/99542/how-to-persist-checkbox-state-if-submission-fails-for-other-reasons-page-refreshes#dis-post-538775 <![CDATA[Re: How to persist checkbox state if submission fails for other reasons & page refreshes?]]> https://forums.modx.com/thread/99542/how-to-persist-checkbox-state-if-submission-fails-for-other-reasons-page-refreshes#dis-post-538755 nuan88 Feb 25, 2016, 12:56 AM https://forums.modx.com/thread/99542/how-to-persist-checkbox-state-if-submission-fails-for-other-reasons-page-refreshes#dis-post-538755 <![CDATA[Re: How to persist checkbox state if submission fails for other reasons & page refreshes? (Best Answer)]]> https://forums.modx.com/thread/99542/how-to-persist-checkbox-state-if-submission-fails-for-other-reasons-page-refreshes#dis-post-538704
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ยข]]>
syberknight Feb 23, 2016, 12:52 PM https://forums.modx.com/thread/99542/how-to-persist-checkbox-state-if-submission-fails-for-other-reasons-page-refreshes#dis-post-538704
<![CDATA[Re: How to persist checkbox state if submission fails for other reasons & page refreshes?]]> https://forums.modx.com/thread/99542/how-to-persist-checkbox-state-if-submission-fails-for-other-reasons-page-refreshes#dis-post-538364
That shouldn't conflict with the persistence since without the checkbox being checked, the form is never submitted.]]>
BobRay Feb 16, 2016, 03:41 AM https://forums.modx.com/thread/99542/how-to-persist-checkbox-state-if-submission-fails-for-other-reasons-page-refreshes#dis-post-538364
<![CDATA[Re: How to persist checkbox state if submission fails for other reasons & page refreshes?]]> https://forums.modx.com/thread/99542/how-to-persist-checkbox-state-if-submission-fails-for-other-reasons-page-refreshes#dis-post-538356
[[!Register?
    &validate=`nospam:blank,
        [b]agree:required:true[/b]`
]]


I could be totally off base here. Checkboxes are weird.]]>
nuan88 Feb 16, 2016, 02:50 AM https://forums.modx.com/thread/99542/how-to-persist-checkbox-state-if-submission-fails-for-other-reasons-page-refreshes#dis-post-538356
<![CDATA[Re: How to persist checkbox state if submission fails for other reasons & page refreshes?]]> https://forums.modx.com/thread/99542/how-to-persist-checkbox-state-if-submission-fails-for-other-reasons-page-refreshes#dis-post-538339 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?]]>
syberknight Feb 15, 2016, 06:44 PM https://forums.modx.com/thread/99542/how-to-persist-checkbox-state-if-submission-fails-for-other-reasons-page-refreshes#dis-post-538339
<![CDATA[Re: How to persist checkbox state if submission fails for other reasons & page refreshes?]]> https://forums.modx.com/thread/99542/how-to-persist-checkbox-state-if-submission-fails-for-other-reasons-page-refreshes#dis-post-538300 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`]] >
]]>
donshakespeare Feb 15, 2016, 12:25 AM https://forums.modx.com/thread/99542/how-to-persist-checkbox-state-if-submission-fails-for-other-reasons-page-refreshes#dis-post-538300