Skip to content
Grav 2.0 is officially stable. Read the announcement →
Plugins

Form or Email Plugin error / Server error

Solved by Karmalakas View solution

Started by Greg 4 years ago · 4 replies · 623 views
4 years ago

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

YAML
---
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 🙂 Thanks

last edited 03/29/22 by pamtbaau
4 years ago Solution

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:

TWIG
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:

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

And not:

YAML
    fields:
        -
            name: name
            label: Name
        -
            name: email
            label: Email
4 years ago

@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.

TXT
-
   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:
    YAML
    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:
    TXT
    "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.
last edited 03/29/22 by pamtbaau
4 years ago

@pamtbaau:
In single quoted strings the single quote character can be escaped by prefixing it with another single quote, basically doubling it.

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

4 years ago

@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

Suggested topics

Topic Participants Replies Views Activity
Plugins · by Rene, 1 week ago
2 75 1 week ago
Plugins · by Xavier, 4 weeks ago
2 81 4 weeks ago
Plugins · by Luka Prinčič, 7 years ago
3 1208 1 month ago
Plugins · by Sebastian van de Meer, 1 month ago
1 75 1 month ago
Plugins · by PIERROT Alain, 2 months ago
3 99 2 months ago