From ab772b4113d46a51733694fb53e6feca23d20a38 Mon Sep 17 00:00:00 2001 From: Tabea David Date: Tue, 26 Sep 2017 13:28:34 +0200 Subject: [PATCH] exclude unchecked checkboxes while counting fields --- CHANGELOG.md | 7 +++++++ SimpleContactForm.module | 15 ++++++++------- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e763df..f4be1fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +### 1.0.5 (2017-09-26) + +- exclude checkbox fields (if unchecked) from spam count + - because it's a standard browser behaviour that the value of a checkbox is only sent if the checkbox was checked + - this leads to a mismatch while counting fields + - the number of submitted fields does not match the number of fields which are present in the form + ### 1.0.4 (2017-09-14) - extend option `classes` diff --git a/SimpleContactForm.module b/SimpleContactForm.module index 8ca43b1..d69254e 100755 --- a/SimpleContactForm.module +++ b/SimpleContactForm.module @@ -10,7 +10,7 @@ use \Jos\Mailer; * See README.md for usage instructions. * * @author Tabea David -* @version 1.0.4 +* @version 1.0.5 * @copyright Copyright (c) 2017 * @see https://github.com/justonestep/processwire-simplecontactform * @see http://www.processwire.com @@ -30,7 +30,7 @@ class SimpleContactForm extends WireData implements Module { return array( 'title' => 'Simple Contact Form', 'summary' => 'Just a simple contact form.', - 'version' => 103, + 'version' => 105, 'href' => 'https://github.com/justonestep/processwire-simplecontactform', 'singular' => true, 'autoload' => true, @@ -194,17 +194,18 @@ class SimpleContactForm extends WireData implements Module { $spamProtector = new SpamProtection(); // exclude markup fields from spam count - $markupFields = 0; + // AND exclude checkbox fields (if unchecked) from spam count + // BECAUSE it's a standard browser behaviour that the value of a checkbox is only sent if the checkbox was checked + $excludeFields = 0; foreach ($this->allFields as $inputfield) { if ($field = $this->fields->get($inputfield)) { - if ($field->type instanceof FieldtypeFieldsetOpen || $field->type instanceof FieldtypeFieldsetTabOpen) { - $markupFields++; - } + if ($field->type instanceof FieldtypeFieldsetOpen || $field->type instanceof FieldtypeFieldsetTabOpen) $excludeFields++; + if ($field->type instanceof FieldtypeCheckbox && !$this->input->post->{$field->name}) $excludeFields++; } } $spamProtector - ->setCount(count($this->allFields) - $markupFields + count(self::$spamFields)) + ->setCount(count($this->allFields) - $excludeFields + count(self::$spamFields)) ->setTimeRange($this->antiSpamTimeMin, $this->antiSpamTimeMax) ->setSaveMessages($this->saveMessages) ->setExcludeIpAdresses($this->antiSpamExcludeIps)