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

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Forms & Blueprints

Form: name="EMAIL" instead of name="data[EMAIL]"?

Started by nicolas 4 years ago · 4 replies · 691 views
4 years ago

Hi,
I’m making a form that has this field:

YAML
        -
            type: email
            label: E-mail
            placeholder: 'Your e-mail address'
            name: EMAIL
            validate:
                required: true

However, it is converted into this HTML:

TXT
<input name="data[EMAIL]" value="" type="email" class=" " placeholder="Your e-mail address" required="required">

Instead of:

TXT
<input name="EMAIL" value="" type="email" class=" " placeholder="Your e-mail address" required="required">

How do I make it name="EMAIL" and not name="data[EMAIL]"?

I’m sending the form through POST to an external service, so it is important for the field name to be just EMAIL and not anything else.

Thanks!

4 years ago

@nclm, A few suggestions:

  • Using your own form
    • Create a plugin that intercepts the submit and pass the posted values to the external service.
  • Using Grav's form plugin (1)
    • Create a plugin which intercepts event 'onFormProcessed`
    • Read the fields and values from the form object supplied to the function.
    • Create your own object.
    • Send the object to the external service.
  • Using Grav's form plugin (2)
    • Try to override the templates of the form plugin
    • My guess is that when changing the field names, other parts of the plugin may break.
4 years ago

Hi, thanks for the quick answer.

So there is no way to choose my own name attribute? This seems like such a basic feature for a form builder.

I have no idea how to create a whole new plugin from scratch and I should get this done in a few minutes, it was meant to be a quick job, adding this simple form.

Unless there is a quick and easy solution (I have the whole form set-up already, it just has to have the right “name” and then I’m done), I will just end up writing the form in HTML in the template.

last edited 03/28/22 by nicolas
4 years ago

Great suggestions from @pamtbaau and I think you have come to the best conclusion.

Simple-to-use form builders are tricky in that using them involves a certain amount of compromise or bending to their ways. The main benefit to using Grav's is its handling IMO (but I say that because I don't struggle at all to create usable, accessible form elements in HTML by hand). If it's a simple form you can code yourself that you are sending to an external target that has expectation of field names, then you're better off crafting the form HTML yourself.

So I'm just affirming your choice 👍

4 years ago

Since the question has been cross-posted on Github, here is a ping back of the answer from the dev team:

[..] from my knowledge, the data[xxx] syntax is just how Grav forms work. There are other internal form hidden inputs that are at the root of the form (nonce, remember-me, etc) and putting the form data inside a specific data[] array ensures we can easily get to that data without having to manually strip out any other extraneous input fields. So it's really just an internal thing so that Grav forms can function reliably.

However, there's nothing stopping you from just creating a form manually in Twig, rather than using Grav forms for your definition. I've done that several times where I just need a simple form that is being sent or processed by an external service (mailinglist signup for example). You create it in the same HTML structure with the same CSS as a Grav form, so the styling matches.

👍 1
last edited 03/29/22 by pamtbaau

Suggested topics

Topic Participants Replies Views Activity
Forms & Blueprints · by Ton Haarmans, 5 years ago
13 1135 4 months ago
Forms & Blueprints · by Hugo Oliveira, 5 months ago
0 60 5 months ago
Forms & Blueprints · by Flachy Joe, 6 months ago
9 132 6 months ago
Forms & Blueprints · by Augustus, 7 months ago
7 108 7 months ago
Forms & Blueprints · by Julien, 7 months ago
10 127 7 months ago