I recently opened PR #470 which tries to fix the default states for checkboxes. I found more issues and it turnes out this is even more complex, so I will try to explain:
Current issues:
use: keys is swapped: https://github.com/getgrav/grav-plugin-form/blob/develop/templates/forms/fields/checkboxes/checkboxes.html.twig#L20
The documentation says:
When set to keys, the checkbox will store the value of the element key when the form is submitted. Otherwise, it will use the element value.
So from my understanding the logic must be changed. However that is not the biggest issue, it just makes it harder to understand.
- Submitting the keys (currently
use: null) and specifying default values is broken and I do not know how to fix this. Let me explain why using this example:
my_field:
type: checkboxes
label: A couple of checkboxes
default:
option1: true
option2: false
options:
option1: Option 1
option2: Option 2
With the current code, option2 is marked as checked. This is because {% set checked = (field.use == 'keys' ? value[key] : key in value) %} will look for key in value. But on a new page load value is filled by field.default which is formatted like this: "option1" => true. value[key] would be correct to use here.
If you now submit the form you will get a different layout of value: "my_field-option1" => option1. Now it would make sense to use key in value instead.
In order to fix this issue we need to know, if value was taken from the defaults or from the form submission. That is possible via {% set realvalue = form ? form.value(field.name) : data.value(field.name) %} or other methods. However what I did not (yet) found possible was to tell if a form was submitted with all checkboxes turned off. A fresh page reload always gives a value of null, but the realvalue from the snippet before will also return null (instead of an empty array).
I assume (but dont know exactly yet) that this is due to the http/form protocol, that is not sending anything to the server, if all checkboxes are empty. We would need to know somehow if a form was already submitted or the page was reloaded with a fresh value. And I have not considered any remember option here...
This is quite tricky, I hope this explained it good enough.
I recently opened PR #470 which tries to fix the default states for checkboxes. I found more issues and it turnes out this is even more complex, so I will try to explain:
Current issues:
use: keysis swapped: https://github.com/getgrav/grav-plugin-form/blob/develop/templates/forms/fields/checkboxes/checkboxes.html.twig#L20The documentation says:
So from my understanding the logic must be changed. However that is not the biggest issue, it just makes it harder to understand.
use: null) and specifying default values is broken and I do not know how to fix this. Let me explain why using this example:With the current code,
option2is marked as checked. This is because{% set checked = (field.use == 'keys' ? value[key] : key in value) %}will look forkey in value. But on a new page loadvalueis filled byfield.defaultwhich is formatted like this:"option1" => true.value[key]would be correct to use here.If you now submit the form you will get a different layout of
value:"my_field-option1" => option1. Now it would make sense to usekey in valueinstead.In order to fix this issue we need to know, if
valuewas taken from the defaults or from the form submission. That is possible via{% set realvalue = form ? form.value(field.name) : data.value(field.name) %}or other methods. However what I did not (yet) found possible was to tell if a form was submitted with all checkboxes turned off. A fresh page reload always gives a value ofnull, but therealvaluefrom the snippet before will also returnnull(instead of an empty array).I assume (but dont know exactly yet) that this is due to the http/form protocol, that is not sending anything to the server, if all checkboxes are empty. We would need to know somehow if a form was already submitted or the page was reloaded with a fresh value. And I have not considered any
rememberoption here...This is quite tricky, I hope this explained it good enough.