Forms, do they work?


#1

Does forms work? is there any examples how to setup a form somewhere? Docs says “Sorry, still in the making…”.


#2

I have a good idea of how forms work. I’ll write some documentation.


#3

@mpartipilo this would be awesome, thank you :ok_hand:


#4

Hi, thanks! Is it possible you could show a simple example on how forms work? (using rest)


#5

It’s done. Go check out the documentation page.


#6

Thanks a lot:)

Btw, does this look correct? I’m getting a 404 on the postUrl.

		postForm() {
			const formData = new FormData(this.$refs.contactForm)
			const postUrl = `${this.cockpit.apiUrl}/forms/submit/Contact?token=${this.cockpit.apiToken}`

      		axios.post(postUrl, formData, {
				headers: { 'Content-Type': 'application/json' },
			}).then((response) => {
				console.log('response: ', JSON.stringify(response, null, 2))
			}).catch((error) => {
                console.log(error)
                console.log(formData)
            })
		}

#7

At a glance, it looks ok.

Let me look for a working example code using axios.


#8

This is how my http request looks from Postman. Axios is not listed in the code generators, but there’s an example with request.

POST /api/forms/submit/MyCockpitForm?token=mytoken HTTP/1.1
Host: content.mywebsite.com
Content-Type: application/json
Cache-Control: no-cache

{
    "form": {
        "field1": "value3",
        "field2": "value4"
    }
}

With request:
var request = require(“request”);

var options = { method: 'POST',
  url: 'http://content.mywebsite.com/api/forms/submit/MyForm',
  qs: { token: 'mytoken' },
  headers: 
   { 'Cache-Control': 'no-cache',
     'Content-Type': 'application/json' },
  body: { form: { field1: 'value3', field2: 'value4' } },
  json: true };

request(options, function (error, response, body) {
  if (error) throw new Error(error);

  console.log(body);
});

#9

thanks, seems my url is wrong in some way but can’t understand what. I can’t access my form json in the browser with:
https://example.com/cockpit/api/forms/get/Contact?token=mytoken

It returns path not found. The exact url structure works for singletons and collections.

EDIT: Nevermind, works now after i did the correct form data structure according to the docs.

Thanks a lot for the docs:)


#10

@artur I was checking the code and it looks like API support in forms is limited to submit only. Can you confirm this?

I could not determine if there is an API to show the form’s entries.


#11

yes, you’re right. the forms api only supports submits


#12

Hello,

How can I receive the forms in my email?


#13

I believe you can set up a webhook? Or you can go programmatically and implement a “forms.save.after” hook in a custom addon, I did something similar in the past for collections in that addon - https://github.com/pauloamgomes/cockpit-EmailOnSave


#14

I can see my form entries, but I’m not getting any emails. Isn’t it enough to fill my email under “email” when editing the form?


#15

Doesn’t it make sense to be able to GET form entries?