Sterc / FormIt

A dynamic form processing Snippet for MODX Revolution
https://docs.modx.com/current/en/extras/formit
33 stars 58 forks source link

Empty Fields Not Processed in emailTpl #118

Closed jpdevries closed 7 years ago

jpdevries commented 7 years ago

Summary

Property tags for fields with no value (like unchecked checkboxes) are not processed when the email is sent

Step to reproduce

Add a checkbox to your form:

<div>
  <input name="useMODX" id="useMODX" type="checkbox" value="Yes I use MODX"/>
  <label for="useMODX">Do you use MODX?</label>
</div>

I can provide a teleport snapshot or MODX login to a blank MODX install with the issue.

In your emailTpl output the checkbox: [[+useMODX]]

Submit the form with the checkbox unchecked, and with it checked. Notice if the checkbox is checked it works correctly. You'll see "Yes I use MODX" in the email. If the checkbox is not checked you will see the escaped MODX tag [[+useMODX]] in the email.

Observed behavior

If the checkbox is not checked you will see the escaped MODX tag [[+useMODX]] in the email.

In /core/components/formit/model/formit/fihooks.class.php I can see that $fields does not contain an element for an unchecked checkbox.

(
    [nospam:blank] => 
    [name] => TEST
    [email] => mail@devries.jp
    [subject] => TEST
    [text] => unchecked
    [numbers] => two
    [nospam] => 
)
(
    [nospam:blank] => 
    [name] => TEST
    [email] => mail@devries.jp
    [subject] => TEST
    [text] => checked
    [numbers] => two
    [useMODX] => Yes I use MODX
    [nospam] => 
)

Expected behavior

If the field has no value the MODX tag should still be parsed, and thus not output anything at all.

Environment

n/a

joeke commented 7 years ago

Hey @jpdevries This is not really a FormIt issue; it's default behaviour in HTML that when you post a form with unchecked checkboxes (or radiobuttons) that that field does not get posted. The proposed fix for this is to include an empty hidden field before the actual checkboxes to always send an empty value when no checkbox is checked. See https://docs.modx.com/extras/revo/formit/formit.tutorials-and-examples/formit.handling-selects,-checkboxes-and-radios for more info on this.

jpdevries commented 7 years ago

Ah ok thanks @joeke