Skip Navigation

[Resolved] Set form Generic field Checkboxes default State in JSON or set default checkboxes options with generic checkboxes field

This thread is resolved. Here is a description of the problem and solution.

Problem:
Set form Generic field Checkboxes default State in JSON or set default checkboxes options with generic checkboxes field

Solution:
The attribute that holds the checked values for checkboxes options with generic checkboxes field is "default".

"default":["1","2"],

You can find proposed solution in this case with the following reply:
https://toolset.com/forums/topic/set-generic-checkboxes-state-in-json/#post-2314241

Relevant Documentation:

This support ticket is created 2 years, 9 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 2 replies, has 2 voices.

Last updated by Dave 2 years, 9 months ago.

Assisted by: Minesh.

Author
Posts
#2314209

Tell us what you are trying to do?

Independently set the state of each of the checkboxes generated from the generic checkboxes field based on the JSON passed to it via shortcode.

I am passing the generic checkboxes field JSON data to populate them like so:

[cred_generic_field type='checkboxes' field='class-child-list']
{
"options":[ [class_children class="in" output="json"] ]
}
[/cred_generic_field]

NOTE: A lot of the php data handling code has been left out of this example as it's not relevant:

function get_class_children( $atts ) {

  //Set the default $atts values
  $defaults = array(
    'id'		=> 	get_the_ID(),
    'class'		=>	NULL,
    'output'	=>	'array'
  );

  //Apply default atts if none have been set
  $atts = shortcode_atts( $defaults, $atts );

  //Use passed 'ID' parameter if one has been passed
  if( $atts['id'] ) {
    $class_id = $atts['id'];
  }

...


    //Initalise array to store results
    $child_list = array();

    //Loop through each child and setup JSON variables
    foreach( $children as $key => $value ) {

      //Child post ID as Value and post title (Child full name) as label
      $child_list[] = array(
        'value' => $value,
        'label' => get_the_title( $value )
      );
    }

    //JSON encode the resulting array and trim off the square brackets to Toolset will recognise it
    $result = trim( json_encode( $child_list ), '[]' );

  }

  return $result;
}

add_shortcode("class_children", "get_class_children");

What I need to do now is add some processing so that if the form is opened again, the checkboxes reflect the previous state. This is easy enough to achieve given the post processing that goes on and the way data is updated, but I can't find a way to pass the state to the checkboxes.

Logically, I assumed the 'state' parameter might work, just like with the Types checkboxes field. So that would look something like this (as a test, the real version would be controlled by a variable obviously):

$child_list[] = array(
        'value' => $value,
        'label' => get_the_title( $value ),
        'state' => 'checked'
      );

This code doesn't error, but it also doesn't seem to do anything as the checkboxes all remain unchecked by default.

Can someone please advise the correct way of passing this data as there is nothing in the documentation regarding the specifics of the generic fields.

Many thanks.

#2314241

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

When I try to generate the checkboxes field with few checked items by default:

[cred_generic_field type='checkboxes' field='test-checkbox-field']
{
"default":["1","2"],
"options":[  
                       {"value":"1","label":"One"},
                       {"value":"2","label":"Two"},
                     {"value":"3","label":"Three"}
              ]
}
[/cred_generic_field]

The attribute that holds the checked values is "default".

"default":["1","2"],

That means, in theory, you will have to have another shortcode that should hold the checked values and you should pass it to "default" attribute.

In your case, it should looks like something:

[cred_generic_field type='checkboxes' field='class-child-list']
{
"default":[ [chekboxes_defaults] ],
"options":[ [class_children class="in" output="json"] ]
}
[/cred_generic_field]
#2314389

Fantastic Minesh, that's perfect thank you!

Just to clarify to anyone reading this, the 'default' property of the checkboxes isn't a default value as I interpreted it to mean, it is a default STATE for the checkboxes and it accepts this data as a comma separated list.

What's even better is that it seems to be pretty smart about the values you pass to it. I've just done some tests and it doesn't care what order you pass the values to it in, if there are some missing, blank or if there are other numbers in there that it can't use, which makes this a lot easier and very usable.

So here is a simple example of taking an array of numbers and passing it to the generic checkboxes field. In my case I am using the post ID's as the values and the labels are the post titles as seen in the php in my first post, so the list I am passing to it is made of the post ID's, which is why the numbers appear random.

function get_registration_checked_status( $atts ) {
  
  //Array of values to be checked by default
  $defaults = array("47","383");
  
  //Convert array into a comma separated list
  $results = implode( ",", $defaults );
  
  return $results;
}
add_shortcode("registration_checked_status", "get_registration_checked_status");

And the generic checkboxes field now looks like this:

[cred_generic_field type='checkboxes' field='class-child-list']
    {
      "default":[ [registration_checked_status] ],
      "options":[ [class_children class="in" output="json"] ]
    }
  [/cred_generic_field]

Hope this helps anybody else with the same question.