Jump to content

Recommended Posts

Posted

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
Posted

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
Posted

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.

  • 1 year later...
Posted

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
Posted

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
Posted

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?

  • 4 weeks later...
  • 2 years later...
Posted

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.

  • 8 months later...
Posted

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
Posted

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

  • Haha 1
Posted
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
Posted

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
  • 1 year later...
Posted

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).

Posted
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.

Posted

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 4
  • Thanks 1
Posted
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. 

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
×
×
  • Create New...