Form or Email Plugin error / Server error

I’m receiving the following error when submitting a form:

key() expects parameter 1 to be array, null given

I’m using the Blog skeleton. I used the Grav suggested frontmatter code but removed the Recaptur stuff, I wanted to get it working… Ha!

Filled in all the Form, email SMTP server guff (my own smtp site server info).
The form crashes with a Server error.

This is the form.md

---
title: Contact
form:
    name: contact-form
    fields:
        -
            name: name
            label: Name
            placeholder: 'Enter your name'
            autofocus: 'on'
            autocomplete: 'on'
            type: text
            validate:
                required: true
        -
            name: email
            label: Email
            placeholder: 'Enter your email address'
            type: text
            validate:
                rule: email
                required: true
        -
            name: message
            label: Message
            size: long
            placeholder: 'Enter your message'
            type: textarea
            validate:
                required: true
    buttons:
        -
            type: submit
            value: Submit
            classes: 'gdlr-button with-border excerpt-read-more'
    process:
        -
            email:
                from: '{{ config.plugins.email.from }}'
                to:
                    - '{{ config.plugins.email.from }}'
                    - '{{ form.value.email }}'
                subject: '[Feedback] {{ form.value.name|e }}'
                body: '{% include ''forms/data.html.twig'' %}'
        -
            save:
                fileprefix: feedback-
                dateformat: Ymd-His-u
                extension: txt
                body: '{% include ''forms/data.txt.twig'' %}'
            message: 'Thank you for your feedback!'
        - null
cache_enable: false
---

Here are the first few entries from the error log:

[2022-03-28 11:32:01] grav.CRITICAL: key() expects parameter 1 to be array, null given - Trace:
#0 /home/micexcha/gmwit.com/system/src/Grav/Common/Debugger.php(843): Whoops\Run->handleError(2, ‘key() expects p…’, ‘/home/micexcha/…’, 930)
#1 [internal function]: Grav\Common\Debugger->deprecatedErrorHandler(2, ‘key() expects p…’, ‘/home/micexcha/…’, 930, Array)
#2 /home/micexcha/gmwit.com/user/plugins/form/classes/Form.php(930): key(NULL)
#3 /home/micexcha/gmwit.com/user/plugins/form/form.php(280): Grav\Plugin\Form\Form->post()

Any advice on what to do is welcome. Please keep in mind I am VERY new to Grav so be clear about what to do, please don’t assume I know where to look :slight_smile: Thanks

May I ask where did you get your form frontmatter example? It’s wrong and especially that last null in the process part. Also what’s with the double single quotes?

It should be more like:

process:
    - email:
        from: "{{ config.plugins.email.from }}"
        to:
            - "{{ config.plugins.email.from }}"
            - "{{ form.value.email }}"
        subject: "[Feedback] {{ form.value.name|e }}"
        body: "{% include 'forms/data.html.twig' %}"
    - save:
        fileprefix: feedback-
        dateformat: Ymd-His-u
        extension: txt
        body: "{% include 'forms/data.txt.twig' %}"
    - message: "Thank you for your feedback!"

Same with fields - it’s better to keep same syntax:

    fields:
        - name: name
          label: Name
        - name: email
          label: Email

And not:

    fields:
        -
            name: name
            label: Name
        -
            name: email
            label: Email

@MurrayG, The error is caused by the last action - null. As the error says, the server expects the key to be an array instead of null.

As @Karmalakas said, the formatting used is not consistent. The ‘message’ action will not work because of this. That action should also be an array entry.

-
   message: 'Thank you for your feedback!'

By the way I wonder why cache_enable: false is being used. Any particular reason?

@Karmalakas,

  • The extra newline in the collection/array format doesn’t make a difference. I agree it would be a tad cleaner without the extra newline.
    By the way, my personal preference would be:
    fields:
      name:
        type: text
        label: Name
      email:
        type: text
        label: Email
    
  • In single quoted strings the single quote character can be escaped by prefixing it with another single quote, basically doubling it.
    The following will are yield the same value:
    "a'b'c"    -- > a'b'c
    'a''b''c'  -- > a'b'c
    a'b'c      -- > a'b'c
    
    That does not hold for double quoted strings. because double quotes are parsed differently.
  • I suspect the sample is taken from the docs Example: Contact Form where OP followed the link Page markdown file, copied the sample pointed at and made a few changes.

Oh, didn’t know that. Is it YAML specific?

@Karmalakas, I’m not familiar with all existing languages, but it holds for YAML. See its specs: 7.3.2. Single-Quoted Style

The single-quoted style is specified by surrounding “ ' ” indicators. Therefore, within a single-quoted scalar, such characters need to be repeated.

1 Like

9 posts were split to a new topic: No email sent by contact form