MODX Community

Extended User Field Checkbox Not Working

I don’t believe that it is a problem with the classExtender plugin. However I added a checkbox and it does not seem to want to populate. The other inputs populate just fine and I have taken the checkbox input name and put it in a placeholder and the value from the database table comes through, but the js for the CMS that adds the hidden input above doesn’t seem to recognize it as a checkbox. Don’t know if I am missing something or if there is an error. This is the first time this has happened to me after using this plugin a few times before with such no problems.

This is the checkbox in the ‘MyExtraUserFields’ chunk:

<div class="x-form-item x-tab-item">
    <div class="x-form-check-wrap" id="ext-gen141">
        <input type="checkbox" autocomplete="off" id="maintMember" name="maintMember" class="x-form-checkbox x-form-field" value="1">
        <label for="maintMember" class="x-form-cb-label">This Customer is currently on the Maintenance Plan</label>
    </div>
</div>

For the sake of being thorough here is the value in the ‘MyExtUserSchema’:

<field key="maintMember" dbtype="tinyint" phptype="boolean" precision="1" attributes="unsigned" default="0" null="false"/>

MODX version: 2.7.3
all associated plugins are update to current versions

If you guys anymore information let me know and Thank You in advance

If you want your checkbox to look and behave like the other ones, you probably have to use Javascript.
Put a wrapper-div into the chunk “MyExtraUserFields” <div id="mycheckbox-wrapper"></div> and then maybe something like this into the plugin:

case 'OnUserFormPrerender':
		$mycheckbox_value = 1;
        
        $modx->regClientStartupHTMLBlock('
        <script type="text/javascript">
    		Ext.onReady(function() {
    		    MODx.load({
    		        xtype: "xcheckbox"
    		        ,renderTo: "mycheckbox-wrapper"
    		        ,id: "mycheckbox"
    		        ,name: "mycheckbox"
    		        ,boxLabel: "My Checkbox Label"
    		        ,inputValue: 1
    		        ,checked: ' . $mycheckbox_value . '
    		    });
    		});
    	</script>');
        break;
1 Like

You might try something like this:

<input type="checkbox" autocomplete="off" id="maintMember" 
name="maintMember" [[!+cb_checked]] 
class="x-form-checkbox x-form-field" value="1">

Then use a snippet (new or existing) to check the value from the DB and replace the cb_checked placeholder with checked if the value is not empty. Be sure the snippet is above the checkbox, and called uncached.

if (!empty ($value) ) {
   $modx->setPlaceholder('cb_checked', 'checked');
}

It would probably work in your plugin too.

1 Like

Maybe this works too, if you use [[+maintMember:is=`1`:then=` checked`]] instead of a snippet.

<input type="checkbox" autocomplete="off" 
id="maintMember" name="maintMember" class="x-form-checkbox x-form-field" value="1"
[[+maintMember:is=`1`:then=` checked`]]>

The problem with this approach compared to the one with Javascript is, that when the checkbox is unchecked, $_POST['maintMember'] is not set on saving.

1 Like

Sry forgot to mention that the checkbox is not updating the value in the database table either, even though the name is correct, so something is not connecting somewhere

Good point, though the snippet or plugin could check the $_POST value and act accordingly.

I’ve found this useful when form values aren’t getting through:

$modx->log(modX::LOG_LEVEL_ERROR, 'POST: ' . print_r($_POST, true)

That will write the $_POST array to the MODX error log. You should get two errors, one when the form loads and one when its submitted.

1 Like

When the user loads:

[2020-10-06 14:51:05] (ERROR @ /home/domain/public_html/core/cache/includes/elements/modplugin/24.include.cache.php : 58) POST: Array ()

When the user is saved:

[2020-10-06 14:51:54] (ERROR @ /home/domain/public_html/core/cache/includes/elements/modplugin/24.include.cache.php : 58) POST: Array
(
[action] => security/user/update
[settings] => {}
[groups] => []
[extended] => {}
[HTTP_MODAUTH] => modx5e7cd9abb6b483.63487189_105f7b6a0d82cb78.09129430
[id] => 48
[username] => username
[fullname] => Test Swanson
[email] => email@email.com
[phone] => 5552220055
[mobilephone] => 
[fax] => 
[address] => 12345 Street
[city] => City
[state] => State
[zip] => 33333
[country] => 
[website] => 
[photo] => 
[dob] => 
[gender] => 0
[newpassword] => false
[primary_group] => 0
[active] => 0
[blocked] => 0
[sudo] => 0
[blockeduntil] => 
[blockedafter] => 
[failedlogincount] => 0
[class_key] => modUser
[comment] => 
[passwordnotifymethod] => s
[passwordgenmethod] => g
[specifiedpassword] => 
[confirmpassword] => 
[fname] => Test
[lname] => Swanson
[maintMember] => 1
[namespace] => core
[area] => 
[filter_key] => Search by key...
[ext-comp-1042] => 20
[extended_name] => 
[extended_value] => 
[extended_id] => 
)

You have to add a hidden input, to be able to reset that checkbox:

<input type="hidden" name="maintMember" value="0">
<input type="checkbox" autocomplete="off" id="maintMember" 
name="maintMember" class="x-form-checkbox x-form-field" 
value="1" [[+maintMember:is=`1`:then=` checked`]]>
1 Like

Yep that works, since the MODX UI javascript didn’t do that automatically on page load like it has every other time on every other site I’ve used it on I still find weird.

Anyways thanks you guys