Jump to content
onjegolders

Checkbox default value

Recommended Posts

Is there any way of presetting a checkbox field?

So that I could have a setting default to true and the user could uncheck it if he/she so wished?

I don't really want to resort to double negatives!

  • Like 1

Share this post


Link to post
Share on other sites

You can do it if using Inputfields from the API, but not from Setup > Fields > some_checkbox_field. Though I'm not sure what you mean about a double negative. ProcessWire is designed to be friendly to schema changes. We assume that you are probably not defining everything you need ahead of time. As a result, it's preferable to use a checked checkbox field to alter a default behavior, not to enforce one.

Lets say you have hundreds of pages using some template, and you decide to add a new checkbox field called enable_nav that you use to determine whether or not to show navigation on a page. You could say that you wanted that checkbox to be checked by default... great, but that only applies to pages created from this point forward. All your existing pages didn't have the field before, and are not checked. So now you have a new problem.

Now consider the the alternative, which would be to make your checkbox do the opposite. Instead of calling it enable_nav, you call it disable_nav. Add it to your template, and your existing pages are kept in the proper default state. New pages created from this point forward will check a box to bypass a default state rather than enable one. Using this strategy scales well, enforces consistency and don't break existing systems when you add a new field.

  • Like 5

Share this post


Link to post
Share on other sites

Thanks for your reply Ryan.

I guess it was just my thinking that if I wanted to do something by default but give the option to turn it off, like a nav or whatever then I'd either have to check it myself manually each time or just leave it unchecked and give the option "disable nav" rather than enable. The question becomes a negative.

But I see your point and overall strategy, it makes sense.

Share this post


Link to post
Share on other sites

I'm missing this feature - in my case I have an "Active" checkbox on some items, and want new items to be active by default. "Inactive" in my case wouldn't make much sense, and I'm also not fond of the double negation... "not inactive" - that's poor semantics.

  • Like 6

Share this post


Link to post
Share on other sites

Just last week I had a case that would've required a "checked by default" checkbox, but ended up with "checking this checkbox means this feature is disabled" approach. Felt ugly, doesn't make much sense.. but on the other hand, adding a new module just to tick one checkbox felt like an unnecessary complication :)

  • Like 1

Share this post


Link to post
Share on other sites

Just last week I had a case that would've required a "checked by default" checkbox, but ended up with "checking this checkbox means this feature is disabled" approach. Felt ugly, doesn't make much sense.. but on the other hand, adding a new module just to tick one checkbox felt like an unnecessary complication :)

A whole module for a single check box tick would be overkill.

But just thinking out loud: A thing like generic "Default Field Values" module would be very useful. It could scan all of the current installation's fields and could offer to put default values in them via API layer :)

But is this even programmatically possible in the first place?

Share this post


Link to post
Share on other sites

this happen in the end as i cant find it in PW3...

Its useful for site editors for usibility (checkboxes shouldnt be used as a double negative as its cognatively confusing so would be useful to have a pre-checked option), not for site builders for the reasons above this comment.

Share this post


Link to post
Share on other sites

I was also just looking for this. I'm using a checkbox where I want it checked for "make all images standard width" which is the default. Changing the text to "make all images non-standard width" would of course be the easy solution, but doesn't make sense to me - the default is they're standard-width, therefore that's the default state, so ON in my mind. Ticking a box to turn it OFF doesn't feel right even if I can wrangle the wording to make it make a little more sense.

@ryan please can this be revisited as per the reasons posted since your last reply? Sometimes the wording can just make more sense to the customer using the system, which is who we're building the sites for at the end of the day :)

  • Like 3

Share this post


Link to post
Share on other sites

As a workaround a select inputfield with one required choice wouldn't do? 

  • Like 1

Share this post


Link to post
Share on other sites

It would - I did think about that but it takes a whole extra minute to set up :D

  • Haha 1

Share this post


Link to post
Share on other sites
On 2/22/2018 at 5:04 PM, Pete said:

Ticking a box to turn it OFF doesn't feel right even if I can wrangle the wording to make it make a little more sense.

I think checkboxes are sometimes confusing to users and replacing them with an on/off switch can go a long way to making it more obvious.

For example, a Yes/No button is more obvious/clear than the assumption that "if this is checked it means yes, and if this is not checked it means no";

For example i'm using this library (incorporated and initialized on some checkbox inputs):

https://github.com/olance/jQuery-switchButton

Here is my initialized checkbox in PW admin:

5a997f34530ac_Edit_Page__Tracy_Settings__localhost.jpg.ad4758da4c5b2ef0838b6e12d20c1bbc.jpg

This still doesn't solve the conundrum of where you want to have it checked by default... the checkbox reversed module is a clever way to solve that...other than js to set the checked attrib..

  • Like 3

Share this post


Link to post
Share on other sites

+1 for selectable default value and yes/no switcher with custom labels :)

  • Like 2

Share this post


Link to post
Share on other sites

Couldn't you just use radio selects, with two options (with whatever labels you need), make it required and have one of the radios selected by default?

  • Like 1

Share this post


Link to post
Share on other sites

Also from me a +1 for Checkbox checked by default property.

I just need to set a "taxable" flag to true by default in an e-commerce module. Otherwise the store owner would need to go and check this checkbox for most products (only some of the products will not be taxable).

Share this post


Link to post
Share on other sites
On 11/24/2012 at 3:01 PM, ryan said:

You can do it if using Inputfields from the API

@ryan how can this be done while creating a field on API side? I tried "defaultValue = 1" property, but didn't work.

Share this post


Link to post
Share on other sites
5 hours ago, bernhard said:

@bernhard I know how to set a value to an InputfieldCheckox but my problem is, I create a Field type=InputfieldCheckbox via API and assign it to a template. If a user creates a new page with this template, the checkbox should be checked by default.

Share this post


Link to post
Share on other sites

What about this?

$wire->addHookAfter("Pages::added", function($event) {
  $page = $event->arguments(0);
  // check for page template if necessary here
  $page->setAndSave('check', 1);
});

checkbox.gif.3756dac3a357794d53c6b3938cc8ddc9.gif

PS: Did you see @Robin S module?

 

  • Like 3

Share this post


Link to post
Share on other sites
45 minutes ago, bernhard said:

What about this?


$wire->addHookAfter("Pages::added", function($event) {
  $page = $event->arguments(0);
  // check for page template if necessary here
  $page->setAndSave('check', 1);
});

 

@bernhard this is great! I already tried it with a hook before field render, but the problem was, it was always set to checked even when unchecked by user. I didn't know the setAndSave method. 

Share this post


Link to post
Share on other sites

Just implemented your solution and it works perfectly!

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By louisstephens
      Is it possible to use count() to return a number of repeater items don't have a checkbox checked? In my current set up, I have a repeater on the page "dev_repeater" with a checkbox called "dev_repeater_exclude". I need to get a count of the current items that do not have it checked so I can pass it to my css grid to alter the column width.
    • By Chris Bennett
      Not sure where I originally saw it while lurking around the forums, but someone, somewhere at some time was asking about styling Uikit checkboxes as toggle-style switches, much like the ones at the bottom of this post asking me if I want to be notified of replies.
      So here is my humble offering, rough and ready,  which can be thrown in at the bottom of your Uikit css as a starting point.
      Everything is based on ems and rems, so you can increase size as you desire by altering the single instance of font-size.
      It only targets single instances of labels within a specific field to a) try to limit unintended consequences and b) because in those cases it often seems more user-friendly to have an on/off binary switch rather than a checkbox. It's still totally a checkbox, just styled differently.
       
      .uk-form-controls-text label:only-of-type input.uk-checkbox { font-size:.8rem; margin-top:0; position:relative; -webkit-appearance:none; outline:none; width:4em; height:2.4em; border:2px solid #D6D6D6; border-radius: 3em; box-shadow:inset 5em 0 0 0 #DDD; flex-shrink: 0; } .uk-form-controls-text label:only-of-type input.uk-checkbox:after { content:""; position:absolute; top:.25em; left:.25em; background:#FFF; width:1.6em; height:1.6em; border-radius:50%; transition:all 250ms ease 20ms; box-shadow:.05em .25em .5em rgba(0,0,0,0.2); } .uk-form-controls-text label:only-of-type input.uk-checkbox:checked { background-color: transparent; box-shadow:inset 5em 0 0 0 #4ed164; border-color:#67bba5; } .uk-form-controls-text label:only-of-type input.uk-checkbox:checked:after { left:1.85em; box-shadow:0 0 1em rgba(0,0,0,0.2); } label:only-of-type input.uk-checkbox:checked + span { color:#008a00; transition:all 250ms ease 20ms; } .InputfieldCheckbox .InputfieldContent label:only-of-type {display:flex;} label:only-of-type input.uk-checkbox + span { color:#c3c3c3; display:flex; align-items:center; line-height:1.1; } /* Below is only necessary if you want the optional "tick" after items when selected */ label:only-of-type input.uk-checkbox + span:after { flex-shrink:0; margin-left:.5em;width:2em; opacity:0; content:url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 250 250'%3E%3Ccircle cx='125' cy='125' r='125' fill='%23231F20' opacity='.1'/%3E%3Cpath fill='%230B8B44' d='M95.823 139.432l-32.492-32.56-31.872 31.883-.008-.008 63.72 63.732L218.549 79.116 187.494 47.52z'/%3E%3C/svg%3E"); } label:only-of-type input.uk-checkbox:checked + span:after { opacity:1; transition: opacity 250ms ease 150ms; }  

    • By Robin S
      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.

       
      https://github.com/Toutouwai/CheckboxReversed
      http://modules.processwire.com/modules/checkbox-reversed/
    • By suntrop
      Hi all! I want to sort child-pages by two fields but I don't know if it is possible within one selector or if I have to make two separate queries
      <?php foreach ($page->children('sort=checkbox, sort=-number') as $child): ?> I want to keep 'checked' pages on top and sort them and all following by the 'number' field. The 'checkbox' field is a 
    • By jploch
      Hi! 
      this should be easy, but I can't get it to work.
      I have a repeater with events.
      Every event has an option-field with multiple checkboxes.
      To filter the events on the frontend I have a script, that uses the class names to filter the results (an event can have multiple categories).

      Now I just want to add every checked option (title) to use as my class name.
      This is what I have so far, wich only gets the first title.
      foreach($page->events as $event) { $tags = $event->options->title; echo "<div class='size1of2 {$tags}'>"; echo "<img src='{$event->image->url}'>"; echo "{$event->text_editor}"; echo "</div>"; }
      I know there is an example with a foreach, but how would I use it in this context?
      foreach($page->countries as $country) { echo "<li>$country->title</li>"; }
       
×
×
  • Create New...