emailCC with FormIt sending via hook, triggers when it should not

So I have a very strange issue, I have a contact form with a option to indicate interest in a other product (using a checkbox, checkboxCCparts). If users check it a CC should be send as with the email.

I use a hook: checkboxCCparts and it works fine every time I test.

But the client keeps sending me mails where the CC is send when the option contact__interested_complete_vehicles (triggers the checkboxCCparts hook) is set to NO, but the CC is still send!
When I test I can not reproduce this at all but he keeps showing me the CC sometimes gets send when it should not.

I have no idea how this is happening, does anybody see a bug? or know a better way to do this?

My Ajax Form

&fsFormTopic=`Parts form`

The form field to trigger CC

<label>[[%drt.contact__interested_complete_vehicles]] [[!+fi.error.contact__interested_complete_vehicles]]</label>
<div class="checkbox_container">
    <input type="hidden" name="contact__interested_complete_vehicles[]" value="" />
    <input type="checkbox" name="contact__interested_complete_vehicles[]" value="[[%drt.contact__yes]]" tabindex="19" [[!+fi.contact__interested_complete_vehicles:FormItIsChecked=`[[%drt.contact__yes]]`]]> [[%drt.contact__yes]]  

checkboxCCparts snippet for the hook

$contact__interested_complete_vehicles = $hook->getValue('contact__interested_complete_vehicles');
if (!$contact__interested_complete_vehicles) {
    $email = ""; 
} else {
    $email = "";
$hook->setValue('emailCC', $email);
return true;

If your input has a name of contact__interested_complete_vehicles[], that means $contact__interested_complete_vehicles will have a value of array('') or array('','yes'). An array like that is a truthy value, meaning that when cast to a boolean it is true, so your hook will always go into the else clause.

I’d recommend changing the form field to not use the array naming (the [] at the end), but simply set it to name="contact__interested_complete_vehicles" on both inputs. That way, the field will have a value of either "" or "yes", and casting "" to boolean results in false, making your hook work as expected.


Thanks Mark will adjust.