Help with checkbox

I have a blueprint that includes a checkbox field:

    header.box:
         type: checkbox
              label: to be tested

But when I make a page in the Admin panel, although the checkbox is displayed, it is always unset when I press Save.

What have I left out?

Did you mean to write “when I press save”? This implies to me you check the checkbox but it’s unset when you go to save the page.

If you mean you want it to default to checked for new pages, use the default field value property. You probably want to set it to ‘checked’ or true, not sure which one works.

Same problem here…
Add type: checkbox to page blueprint.
In Admin select checkbox.
Press save
Page reload
=> checkbox is unset.

Is it a typo ?
Because it should be:

header.box: 
  type: checkbox 
  label: to be tested

@seb3000, I cannot reproduce your issue…

I did the following:

  • Create file ‘user/blueprints/pages/default.yaml’
  • I copied the first page blueprint example from the docs Example: Page Blueprints into above file
  • Copied the code from OP (and correctly indented it) below the existing field from the example resulting in the following (partial) blueprint:
    [stuff deleted]
    
    fields:
      header.an_example_text_field:
        type: text
        label: Add a number
        default: 5
        validate:
          required: true
          type: int
      header.box:
        type: checkbox
        label: to be tested
    
  • The page editor on tab ‘Advanced’ and below section ‘Overrides’ will look like:
    image
  • I checked the box and saved. The saved and refeshed page looks like:
    image
  • The content of the page’s frontmatter:
    ---
    title: Home
    body_classes: 'title-center title-h1h2'
    an_example_text_field: 5
    box: '1'
    ---
    

@seb3000, would you mind sharing a reproducible case showing the issue?

By the way, the following definition will yield a value of false/true in frontmatter:

header.box:
  type: checkbox
  label: to be tested
  validate:
    type: bool

@AmauryH, I don’t think the indentation used by OP is actually in the code, because that indentation makes the yaml invalid and causes Grav to throw an Exception ‘Unable to parse at line …’

Thanks for your answer. It seems like checkboxes are working fine when using standalone, but not if used inside a list.

Have a look at my blueprint snippet

  fields:
    content:
      unset@: true
      type: tab
      title: Liste mit Bild
      fields:

        header.title:
          type: text
          label: Titel
          validate:
            required: true

        header.list:
          type: list
          label: Liste
          fields:

            .title:
              type: text
              label: Überschrift

           .agree_to_terms:
              type: checkbox
              label: "Agree to the terms and conditions"
              validate:
                required: true

My workaround was using the toggle field, which is working inside nested lists.

  .plain:
    type: toggle
    label: "Als Überschrift anzeigen?"
    highlight: 1
    help: PLUGIN_ADMIN.ENABLED_HELP
    options:
      1: PLUGIN_ADMIN.YES
      0: PLUGIN_ADMIN.NO
    validate:
      type: bool

@seb3000,

  • I have added you checkbox snippet to Quark’s ‘blueprints/default.yaml’
  • Fixed indentation on line .agree_to_terms:
  • Admin page now looks like the following after adding a row into the list:
    image
  • Saved page
  • Header of ‘default.md’ has the following list added:
    list:
      -
        title: Heading
        agree_to_terms: '1'
    
  • Adding multiple rows is working fine too.

What am I doing wrong?

Note

  • Adding validate: type: bool would store values true/false in the header of the page, which seems more appropriate for a checkbox.