Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contact form submission not saved due to validation error #249

Closed
LouisCuvelier opened this issue Jun 9, 2023 · 14 comments
Closed

Contact form submission not saved due to validation error #249

LouisCuvelier opened this issue Jun 9, 2023 · 14 comments
Labels

Comments

@LouisCuvelier
Copy link

Hi,

Description

I can't make this plugin working since upgrading to Craft v4. The email is never send due to validation error.

Steps to reproduce

Firstly I thought my Twig code was wrong, so I copied/pasted the exact same example in readme on my page :

{% macro errorList(errors) %}
  {% if errors %}
    {{ ul(errors, {class: 'errors'}) }}
  {% endif %}
{% endmacro %}

{% set submission = submission ?? null %}

<form method="post" action="" accept-charset="UTF-8">
  {{ csrfInput() }}
  {{ actionInput('contact-form/send') }}
  {{ redirectInput('contact/merci') }}

  <h3><label for="from-name">Your Name</label></h3>
  {{ input('text', 'fromName', submission.fromName ?? '', {
    id: 'from-name',
    autocomplete: 'name',
  }) }}
  {{ submission ? _self.errorList(submission.getErrors('fromName')) }}

  <h3><label for="from-email">Your Email</label></h3>
  {{ input('email', 'fromEmail', submission.fromEmail ?? '', {
    id: 'from-email',
    autocomplete: 'email',
  }) }}
  {{ submission ? _self.errorList(submission.getErrors('fromEmail')) }}

  <h3><label for="subject">Subject</label></h3>
  {{ input('text', 'subject', submission.subject ?? '', {
    id: 'subject',
  }) }}
  {{ submission ? _self.errorList(submission.getErrors('subject')) }}

  <h3><label for="message">Message</label></h3>
  {{ tag('textarea', {
    text: submission.message ?? '',
    id: 'message',
    name: 'message',
    rows: 10,
    cols: 40,
  }) }}
  {{ submission ? _self.errorList(submission.getErrors('message')) }}

  <button type="submit">Send</button>
</form>

But the email never sends and in the log I still have this error :
2023-06-09 09:40:24 [web.INFO] [craft\contactform\Mailer::send] Contact form submission not saved due to validation error. {"memory":3736880}

I tried to debug a bit and It seems to come from the function send() (line 53 in SendController.php) that is calling the function validate() (line 47 of Mailer.php)

When I comment the line 49 in Mailer.php, the emailing is sending correctly. So my SMTP settings are ok.

Any idea of what is happening ?

Additional info

  • Craft version: 4.4.13
  • PHP version: 8.1.19
  • Database driver & version: 8.0.33
  • Plugins & versions: 3.0.1
@seanconn
Copy link

seanconn commented Jun 13, 2023

Having this issue also. Removing validation on lines 47 - 50 of mailer.php also resolving issue for me.

Carft version: 4.4.13
PHP version: 8.1.15
Contact-form version 3.0.1

@fvwanja
Copy link

fvwanja commented Jun 14, 2023

@LouisCuvelier did you double check that you send a value for

  • a field with name="fromEmail"
  • a field with name="body"

These fields are a requirement for the plugin to work properly (I think this could be better emphasized in the README @pi– and I think submitting a form without an E-Mail field should also be possible).

I happened to stumble across the same behaviour today – debugging revealed there was an actual problem here...

Running on 3.0.1 with Craft 4.4.12 with PHP 8.0.23.

Screenshot-2023-06-14-at-10 54 01-cleaned

@LouisCuvelier
Copy link
Author

@fvwanja Yes I did.
And when I dug in the logs, I saw that subject should be more than 40 characters and body should be more than 70 characters but there is no verification on the plugin side.

@angrybrad
Copy link
Member

Just installed Contact Form on a fresh Craft 4.4.14 installation and used the OP’s template, and I’m not able to reproduce this.

The form successfully submits with no validation errors, and required field validation seems to work as expected, and the example form error handling logic seems to pick it up fine.

CleanShot 2023-06-15 at 17 29 36

If someone can provide more details/steps in case we’re missing something, let us know, and we'll look into it further. As it stands, I’m not 100% sure what an actionable item is we could do here.

@fvwanja
Copy link

fvwanja commented Jun 16, 2023

Hey @angrybrad 👋

Thanks for dropping in ☺️

You could improve the documentation in https://github.com/craftcms/contact-form/blob/main/README.md as well as the validation on what is required for the forms to actually work – and most of all why the requirements exist 👍 much appreciated!

For example tell us about any required fields like fromEmail and body and inform us about stuff like a minimum length (like mentioned in #249 (comment)). Maybe there is even more to be found 😉 It would be nice to know about those restrictions before the first user fails to submit any data.

@LouisCuvelier can you try different versions like 3.0.0-beta.2 from #249 (comment) and see if that fixes your issue?

@LouisCuvelier
Copy link
Author

@fvwanja Nop, downgrading doesn't solve the issue.

Also, I've updated to Craft 4.4.14. It changed nothing.

@angrybrad here's my full composer.json if It can help maybe ?

{
  "require": {
    "craftcms/anchors": "3.2.0",
    "craftcms/cms": "4.4.14",
    "craftcms/contact-form": "3.0.1",
    "craftcms/redactor": "3.0.4",
    "craftsnippets/table-of-contents": "2.0.0",
    "doublesecretagency/craft-cpcss": "2.6.0",
    "ether/simplemap": "4.0.3",
    "jalendport/craft-readtime": "2.0.0-beta.1",
    "matt-west/craft-recaptcha": "2.0.2",
    "mmikkel/retcon": "2.6.1",
    "nystudio107/craft-cookies": "4.0.0",
    "nystudio107/craft-disqus": "4.0.0",
    "nystudio107/craft-eagerbeaver": "4.0.2",
    "nystudio107/craft-emptycoalesce": "4.0.0",
    "nystudio107/craft-minify": "4.0.0-beta.2",
    "nystudio107/craft-retour": "4.1.12",
    "nystudio107/craft-seomatic": "4.0.28",
    "nystudio107/craft-templatecomments": "4.0.0",
    "nystudio107/craft-twigpack": "4.0.0-beta.4",
    "nystudio107/craft-typogrify": "4.0.1",
    "verbb/consume": "1.0.3",
    "verbb/field-manager": "3.0.6",
    "verbb/image-resizer": "3.0.6",
    "verbb/super-table": "3.0.9",
    "vlucas/phpdotenv": "^5.4.0",
    "workingconcept/craft-cloudflare": "2.0.0",
    "wrav/oembed": "2.3.0"
  },
  "autoload": {
    "psr-4": {
      "modules\\": "modules/"
    }
  },
  "config": {
    "optimize-autoloader": true,
    "sort-packages": true,
    "allow-plugins": {
      "yiisoft/yii2-composer": true,
      "craftcms/plugin-installer": true
    },
    "platform": {
      "php": "8.0.2"
    }
  },
  "scripts": {
    "post-root-package-install": [
      "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
    ],
    "post-update-cmd": [
      "@php craft migrate/all",
      "@php craft project-config/apply",
      "@php craft clear-caches/temp-files",
      "@php craft clear-caches/compiled-templates",
      "@php craft clear-caches/cp-resources",
      "@php craft clear-caches/data",
      "@php craft clear-caches/seomatic-frontendtemplate-caches",
      "@php craft clear-caches/seomatic-metabundle-caches",
      "@php craft clear-caches/seomatic-sitemap-caches",
      "@php craft clear-caches/retour-redirect-caches",
      "@php craft cloudflare/purge/purge-all"
    ],
    "post-install-cmd": [
      "@php craft migrate/all",
      "@php craft project-config/apply",
      "@php craft clear-caches/temp-files",
      "@php craft clear-caches/compiled-templates",
      "@php craft clear-caches/cp-resources",
      "@php craft clear-caches/data",
      "@php craft clear-caches/seomatic-frontendtemplate-caches",
      "@php craft clear-caches/seomatic-metabundle-caches",
      "@php craft clear-caches/seomatic-sitemap-caches",
      "@php craft clear-caches/retour-redirect-caches",
      "@php craft cloudflare/purge/purge-all"
    ]
  }
}

@seanconn
Copy link

Hey @angrybrad, @fvwanja fvwanja

Did you try testing with smtp enabled? The issue does not seem to occur with sendmail.

@fvwanja
Copy link

fvwanja commented Jun 20, 2023

@seanconn yes SMTP is enabled in my case.

@angrybrad
Copy link
Member

@fvwanja

You could improve the documentation in https://github.com/craftcms/contact-form/blob/main/README.md as well as the validation on what is required for the forms to actually work – and most of all why the requirements exist 👍 much appreciated!

Fair - there is a mention here: https://github.com/craftcms/contact-form#usage

The only required fields are fromEmail and message. Everything else is optional.

Although I suppose it could be called out more. Those two fields (fromEmail and message) are required because an email needs a reply to address and body at a minimum.

@LouisCuvelier

and when I dug in the logs, I saw that subject should be more than 40 characters and body should be more than 70 characters but there is no verification on the plugin side.

That’s not a Contact Form requirement. All we require is that fromEmail and message are required and that fromEmail is an email address. See: https://github.com/craftcms/contact-form/blob/main/src/models/Submission.php#L66-L67

There are plenty of Contact Form plugins in the plugin store that hook into Contact Form events. Maybe you’ve got one installed that is adding extra validation or it’s coming from a custom module?

@LouisCuvelier
Copy link
Author

@angrybrad

There are plenty of Contact Form plugins in the plugin store that hook into Contact Form events. Maybe you’ve got one installed that is adding extra validation or it’s coming from a custom module?

You are the only one. You have the composer.json in this thread above.
And, the timing of this log is exactly when I clicked on "submit" form.

@angrybrad
Copy link
Member

@LouisCuvelier sorry... I meant other plugins that extend our Contact Form plugin.

If you’d like to send a database dump, your composer.json, and composer.lock files, and a front-end template over to support@craftcms.com along with steps to reproduce what you’re seeing, then we can dig in further.

@LouisCuvelier
Copy link
Author

@angrybrad done ✅ Thanks

@angrybrad
Copy link
Member

angrybrad commented Jun 26, 2023

@LouisCuvelier Your issue comes from the matt-west/craft-recaptcha plugin you've installed. It listens for submissions from Craft’s contact form plugin, and when it fails its call to Google to see if its valid, it returns it as a generic validation error to the contact form plugin.

If you disable that plugin, the issue goes away for me.

@LouisCuvelier
Copy link
Author

Annnnd you're right ! Thanks for the help 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants