1

Using Freeform, I am wanting to have a checkbox on a front end form that is not checked by default that posts 'Yes' if checked and 'No' if not checked. I am creating a manual HTML form that works fine for all the other fields, but I can't figure out what to write for the checkbox logic.

From the docs I know I can set the value in the back end to a default of either 'Yes' or 'No', I have done this and using the following template code but it always sends through the same value of either Yes or No depending on what you set in the back end.

<input type="checkbox"
        name="{{ receiveContactFromTFA.handle }}"
        value="{{ receiveContactFromTFA.value == 'Yes' ? 'Yes' : 'No' }}"
        {% if receiveContactFromTFA.value == 'Yes' %} checked{% endif %}>

I have also just tried not having a value attribute at all but again the value defaults to yes because it is set in the back end. I have also tried removing the value entirely and having the following template code:

<input type="checkbox"
        name="{{ receiveContactFromTFA.handle }}">

If I do that then then nothing posts. I know a checkbox will normally post a '1' if checked but how do you handle this logic using Freeform to be either 'Yes' or 'No'?

| improve this question | | | | |
2

I don't think this has anything to do with your code, and actually appears to be a bug in Freeform itself, where the Submission object and email notifications are showing the value as whatever the default is set to.

I've made a note about this issue and we'll have it corrected shortly. :)

| improve this answer | | | | |
  • Thank you for looking into this. Once I've got it working with the bug fix I'll post my code here for completeness. – Gareth Sep 13 '17 at 4:59
2

This still appears to be an issue with checkboxes on Freeform Craft 1.6.2. The mailing list checkbox is repopulating as expected but the checkbox field is not repopulating in a multi-step form after submitting and going back. All fields have a value of 1.

| improve this answer | | | | |
0

The bug was fixed for me in 1.6.2 my final code was just a simple:

<input type="checkbox"
        name="{{ receiveContactFromTFA.handle }}"
        value="{{ receiveContactFromTFA.value }}"
        {% if receiveContactFromTFA.value == 'Yes' %} checked{% endif %}>
| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.