Jump to content

Why is a value required to pre-check checkboxes?


cjx2240
 Share

Recommended Posts

I want to have a list of checkboxes and check a couple by default. However, I want to allow my users to uncheck them if they like.

This seems impossible, am I missing something?

Quote
  •  <field title> (<field_name>): Configured pre-selection not populated since value is not always required. Please correct this field configuration.

 

  • Like 1
Link to comment
Share on other sites

Yeah I get that, but then they have to tick an option.

Lets say I have 10 options, I want to make things easier by pre-checking some of the options which are most likely to be checked.

However, I also want the user to have the option of ticking nothing.

If I make it required and set default values, then the user unticks everything, it doesn't accept the change on Save.

I also don't understand the point of selecting default values in Input and pre-selected options in Details but that's not the point...

Link to comment
Share on other sites

Hi @cjx2240

Here is Ryan's note about that: 

The question is an easy one when talking about forms that get submitted once, but not so easy when talking about forms that edit existing pages and will continue to do so indefinitely in the future. That's why you see in initial value support in many Inputfields that doesn't apply when used in a page/field context. Meaning, you see the option when using an Inputfield in FormBuilder, but not when using the same Inputfield for editing a page field.

When it comes to this FieldtypeOptions module, initial value support (pre-selected options) needs "required" status for the Inputfield because it has to have some way to know when to populate the initial value. It knows to populate the initial value when there is no selection present. There isn't any other straightforward way for it to make that determination, unless we limit the feature only to newly created pages.

If the user were to de-select everything so that no options are selected, then the initial value would once again get populated the next time they edited the page. That's why we only support initial values if "required" status is active, as it prevents the possibility of the user de-selecting all options and assuming it will stay that way.

There is an easy way around it though. Create an option to represent "no selection", likely your first option. If the user literally wants no selection, they would select that, and still meet the "required" status of the field. From there you can have any pre-selected option(s) you want. Since you as the site developer put that option there, you can likewise know when to act–or not act–upon it in your output code.

  • Like 4
Link to comment
Share on other sites

Is it about fields in the admin/backend?

A hack could be to preselect the desired checkboxes with a hook on Pages::added()

Assuming it is only relevant to have the preselection on new pages. Which would go into the direction of Zekas quote of Ryan above:

Quote

There isn't any other straightforward way for it to make that determination, unless we limit the feature only to newly created pages.

 

  • Like 2
Link to comment
Share on other sites

Actually, I just remembered that I made a module which just aims for that, but relying on the new pages to inherit those "base settings" from parents. :rolleyes:

https://github.com/blynx/AdoptDefaultsFromParents

What it is about:

In the module settings, you can define templates that hand over the values of fields (which you also have to define) to newly created pages below them. Also, you can decide whether the new pages inherit the values only from the immediate parent or from the closest parent with that field.

I used it for a shop - so that products inherit some values from their product-category (parent page). The product-category defines some basic common settings for the products then ...

Yet, I haven't tested it with all sorts of fields - it might work well with that checkboxes field (options?) - it works with pagefields, simple texts, etc ... but not for images / files ... it simply copies the field value via setAndSave()

You can have a look at the code there on the github page for inspiration - or even use the module if it suits you ;)
Or, here is the basic code to put into an /site/init.php file (see: https://processwire.com/blog/posts/processwire-2.6.7-core-updates-and-more/)

<?php

wire('pages')->addHookAfter('Pages::added', null, function() {
	
    $page = $event->arguments(0);
    
    if($page->template->name == 'my-template-name') {
        // save outputformatting state
        // and set to false
        $of = $page->of();
        $page->of(false);
        
        $desired_value = ## my desire ## 
      
        // set the desired field value
        $page->set('that-field-name', $desired_value);
        
        // save page
        // and re-set of to initial state
        $page->save();
        $page->of($of);
    }
});

cheers!

  • Like 2
Link to comment
Share on other sites

10 hours ago, cjx2240 said:

Yeah I get that, but then they have to tick an option.

Lets say I have 10 options, I want to make things easier by pre-checking some of the options which are most likely to be checked.

However, I also want the user to have the option of ticking nothing.

If I make it required and set default values, then the user unticks everything, it doesn't accept the change on Save.

I also don't understand the point of selecting default values in Input and pre-selected options in Details but that's not the point...

You can just add a 'None' option as a selection for user to tick nothing and manipulate it in your code

Link to comment
Share on other sites

17 hours ago, blynx said:

Actually, I just remembered that I made a module which just aims for that, but relying on the new pages to inherit those "base settings" from parents. :rolleyes:

https://github.com/blynx/AdoptDefaultsFromParents

 

My friend, this works perfectly and right out of the box. It solves my issue even better than pre-selecting options on a per-field basis.

I would like to buy you a beer!

  • 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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...