Jump to content

Checkbox Reversed


Robin S
 Share

Recommended Posts

Checkbox Reversed

Modifies InputfieldCheckbox so that it shows the reverse of its true value. The checkbox will be unchecked when the field value is 1 and checked when the field value is not 1.

Background

The core FieldtypeCheckbox does not have a setting that allows a checkbox to be checked by default. One reason for this is that only a checked field saves a value to the database. An unchecked field does not save "0" to the database, but rather does not save any value for the field at all. Therefore there is no way to distinguish between a new field that has not yet been saved (and therefore could potentially get a default checked state) and a field that has deliberately been saved as unchecked.

Because of this you sometimes have to use a checkbox in the opposite way than you would like. Suppose your client has requested a checkbox labelled "Bootylicious" that will be checked by default. This isn't possible with FieldtypeCheckbox so instead you have to convince them that a checkbox labelled "Not bootylicious" that is unchecked by default is just as good. This alternative will achieve the same thing, but it's not ideal.

A solution

This module doesn't change the limitations of the core checkbox field, but it provides a workaround that allows you to show the checkbox with the desired default state and label. So in the example above you would still name the field "not_bootylicious" (otherwise it could get confusing in your template files) but you can label the field "Bootylicious" and the checkbox will appear checked when its true value is actually unchecked, and vice versa. This allows new pages to show the checkbox checked by default. Clear as mud? :)

Usage

Install the Checkbox Reversed module.

For any Checkbox field where you want the inputfield to show the reverse of its true value, activate the "Reverse the checked state of this inputfield?" option in the field settings.

checkbox-reversed-setting

 

https://github.com/Toutouwai/CheckboxReversed
http://modules.processwire.com/modules/checkbox-reversed/

  • Like 11
Link to comment
Share on other sites

  • 1 year later...

Hey Robin, I wonder what you think about this approach:

This could also be packed in a module and I think the behaviour is a little bit easier to understand (checked is checked ? ). Maybe you tried something similar and found some drawbacks with this simple approach?

Link to comment
Share on other sites

1 hour ago, bernhard said:

Maybe you tried something similar and found some drawbacks with this simple approach?

A drawback to that approach is that users will not necessarily only add the field to templates that don't yet have any pages - they could add the field to templates that have many existing pages and then confusion results when they see that their pages don't have the checkbox checked by default. Ryan discussed this here:

And a module can't get around this problem by itself because of what I mentioned in the readme:

Quote

An unchecked field does not save "0" to the database, but rather does not save any value for the field at all. Therefore there is no way to distinguish between a new field that has not yet been saved (and therefore could potentially get a default checked state) and a field that has deliberately been saved as unchecked.

So some separate API script needs to be run as needed to populate the checked state on pages immediately after the field is added to a template. But certainly you could create a module around this approach and perhaps include an example script that users could refer to as a starting point if they feel confident using the API.

  • Thanks 1
Link to comment
Share on other sites

Thx Robin! ? @Gadgetto this might also be interesting for you.

I still think having the value reversed and naming the field not_whatsoever is not the best solution (please don't take this as criticism!). Couldn't we create a new Fieldtype "CheckboxDefaultTrue" that extends the 

I've played around with this idea and I got this "far" ?

<?php namespace ProcessWire;

/**
 * ProcessWire Checkbox Fieldtype
 *
 * This Fieldtype stores an ON/OFF toggle via a single checkbox. The ON value is 1 and OFF value is 0.
 *
 * For documentation about the fields used in this class, please see:  
 * /wire/core/Fieldtype.php
 * 
 * ProcessWire 3.x, Copyright 2016 by Ryan Cramer
 * https://processwire.com
 *
 */

class FieldtypeCheckboxTrue extends FieldtypeCheckbox {

	public static function getModuleInfo() {
		return array(
			'title' => 'CheckboxTrue',
			'version' => 101,
			'summary' => 'Same as core checkbox but defaults to true',
			);
	}

	public function getBlankValue(Page $page, Field $field) {
		return 1; 
	}

	public function sanitizeValue(Page $page, Field $field, $value) {
		return $value ? 1 : 0; 
	}

	public function getMatchQuery($query, $table, $subfield, $operator, $value) {
		// need help here :)
	}

}

This seems to work, but I'd need help with the getMatchQuery. This way the checkbox stores "0" for every unchecked checkbox and NULL for every checked checkbox (which is the default):

D71CCs0.png

xRNYHZx.png

What do you think?

Link to comment
Share on other sites

Hm... Thinking a little more about it, I think having a setting "set checkbox to true by default for newly created pages" would be the best option. Changing the default setting during lifetime of your website is unlikely and if so, this setting would just impact newly created pages and there would not be the need for updating existing pages (as they already have the correct value, which was set manually at that time). It would be a very easy hook, as shown above: https://processwire.com/talk/topic/2199-checkbox-default-value/?tab=comments#comment-184564

Sorry for hijacking your thread robin and thanks for the discussion ? 

 

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...