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

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

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

However, it is converted into this HTML:

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

Instead of:

<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!

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

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.

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 :+1:

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 Like