I'm using Sprout Forms and Craft CMS 3.4.5. My client wants the email notification to go to different email addresses based on whether or not a form checkbox is checked. My plan is to create a new notification for each checkbox value, with the checkbox logic in a custom rule. I've read through the documentation, but I can't figure out how to write the code that checks whether a checkbox is checked. I think it's something like { checkBoxFieldHandle == 'value' } or { checkBoxFieldHandle.valueBeingChecked.selected } but neither of those are sending any notifications after a test message. Any advice on how to do this? Thank you!


You're on the right track.

The first thing to note is that the Send Rule field does support shorthand syntax like in your question, however, if you are using compound logic, you will want to use the full object syntax for that logic.

So, instead of (with no reference to object. and single curly braces):

{ checkBoxFieldHandle == 'value' }

You'll want to use the full object syntax to get the expression to evaluate:

{{ object.checkBoxFieldHandle == 'value' }}

The next thing to note is that the checkbox field is a multi-select input, meaning more than one value can be selected. This could be present additional logic challenges for what you are trying to do but assuming you have a checkboxes field with a single checkbox or an opt-in field the logic is more predictable.

Checkboxes are submitted as arrays, so you'll need to make sure you are targeting the correct node in the array. In the case of a single checkbox, we know the first checkbox will be index 0 in the array. Once we have the target checkbox, we can see if it's selected by checking the selected attribute:

{{ object.checkboxFieldHandle.getOptions()[0].selected == true }}

And we can update the same statement to check for false if we want to only send if the checkbox is unchecked:

{{ object.checkboxFieldHandle.getOptions()[0].selected == false }}
| improve this answer | |
  • Thanks for the advice! I've added {{ object.checkboxFieldHandle[0].selected == true }} but now the notification is always being sent, even when I didn't check the [0] index checkbox. Side note: the default notification template for checkboxes is sending the checkbox handle, rather than the label; for example, I'm receiving "Checkbox Field Name: checkboxFieldHandle" in the notification email, rather than "Checkbox Field Name: Checkbox Field Label." I can override that myself with a custom template, but is that a bug with the default template? – Jon Weedin Feb 12 at 16:51
  • 1
    I've updated my answer with a new syntax that works in my tests: {{ object.checkboxFieldHandle.getOptions()[0].selected == true }} – Ben Parizek Apr 10 at 11:46
  • This worked! Thank you so much! – Jon Weedin Apr 10 at 16:59

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.