Submitting a form
✅ Purpose of the scenario:
- Get form configuration from OneEntry CMS
- Collect user input data from a contact form.
- Ensure data is correctly formatted and validated before submission.
- Submit the collected data to an OneEntry API.
✅ What you need:
- A valid PROJECT_URL and APP_TOKEN for authentication with the OneEntry API.
- A pre-configured form in OneEntry identified by a marker (e.g., 'contact_us').
- A pre-configured form fields.
📌 Important:
- Handle potential errors during API requests gracefully to provide feedback to users.
- Maintain data privacy and security, especially when handling sensitive information like email addresses.
- Keep the form fields and their markers in sync with the backend configuration to avoid mismatches.
- We do not handle errors in these examples.
- You can handle errors in trycatch or in a construction like "await Promise.catch((error) => error)"
Scenario
1. Import oneEntry and define url and token
Example:
import { defineOneEntry } from 'oneentry';
const PROJECT_URL = 'your-project-url';
const APP_TOKEN = 'your-app-token';
2. Creating an API client with defineOneEntry() function
Example:
const { AuthProvider, Orders, Payments, Forms } = defineOneEntry(PROJECT_URL, {
token: APP_TOKEN,
});
3. Get form with Forms.getFormByMarker()
Example:
const formData = await Forms.getFormByMarker('contact_us');
Result:
[
{
"type": "string",
"marker": "first_name",
"isLogin": null,
"isSignUp": null,
"position": 1,
"settings": {},
"isVisible": true,
"listTitles": [],
"validators": {
"requiredValidator": {
"strict": true
}
},
"localizeInfos": {
"title": "First name"
},
"additionalFields": [],
"isNotificationEmail": null,
"isNotificationPhoneSMS": null,
"isNotificationPhonePush": null
},
{
"type": "string",
"marker": "email",
"isLogin": null,
"isSignUp": null,
"position": 2,
"settings": {},
"isVisible": true,
"listTitles": [],
"validators": {
"requiredValidator": {
"strict": true
},
"emailInspectionValidator": true
},
"localizeInfos": {
"title": "Email"
},
"additionalFields": [],
"isNotificationEmail": null,
"isNotificationPhoneSMS": null,
"isNotificationPhonePush": null
},
{
"type": "string",
"marker": "surname",
"isLogin": null,
"isSignUp": null,
"position": 3,
"settings": {},
"isVisible": true,
"listTitles": [],
"validators": {
"stringInspectionValidator": {
"stringMax": 0,
"stringMin": 0,
"stringLength": 0
}
},
"localizeInfos": {
"title": "Surname"
},
"additionalFields": [],
"isNotificationEmail": null,
"isNotificationPhoneSMS": null,
"isNotificationPhonePush": null
},
{
"type": "list",
"marker": "topic",
"isLogin": null,
"isSignUp": null,
"position": 4,
"settings": {},
"isVisible": true,
"listTitles": [
{
"title": "Article",
"value": "article",
"extended": {
"type": null,
"value": null
},
"position": 1
},
{
"title": "Article-2",
"value": "article-2",
"extended": {
"type": null,
"value": null
},
"position": 2
}
],
"validators": {
"requiredValidator": {
"strict": true
}
},
"localizeInfos": {
"title": "Topic"
},
"additionalFields": [],
"isNotificationEmail": null,
"isNotificationPhoneSMS": null,
"isNotificationPhonePush": null
},
{
"type": "text",
"marker": "text",
"isLogin": null,
"isSignUp": null,
"position": 5,
"settings": {},
"isVisible": true,
"listTitles": [],
"validators": {},
"localizeInfos": {
"title": "Text"
},
"additionalFields": [],
"isNotificationEmail": null,
"isNotificationPhoneSMS": null,
"isNotificationPhonePush": null
},
{
"type": "spam",
"marker": "spam",
"isLogin": null,
"isSignUp": null,
"position": 6,
"settings": {},
"isVisible": true,
"listTitles": [],
"validators": {},
"localizeInfos": {
"title": "You're not a robot?"
},
"additionalFields": [],
"isNotificationEmail": null,
"isNotificationPhoneSMS": null,
"isNotificationPhonePush": null
},
{
"type": "button",
"marker": "send",
"isLogin": null,
"isSignUp": null,
"position": 7,
"settings": {},
"isVisible": true,
"listTitles": [],
"validators": {},
"localizeInfos": {
"title": "Send"
},
"additionalFields": [],
"isNotificationEmail": null,
"isNotificationPhoneSMS": null,
"isNotificationPhonePush": null
}
]
4. Get fields data from frontend Form
Example:
[
{
"marker": "first_name",
"value": "Firstname"
},
{
"marker": "surname",
"value": "Surname"
},
{
"marker": "email",
"value": "your@email.com"
},
{
"marker": "topic",
"value": [
{
"title": "Article",
"value": "article"
}
]
},
{
"marker": "text",
"value": "Some text"
}
]
5. Transform data for use with OneEntry Form
Example:
const transformedFormData = fieldsData.map((field: any) => {
const { marker, value } = field;
const formField = formFields.find(
(field: { marker: string }) => field.marker === marker,
);
const { type } = formField;
switch (type) {
case 'text':
return {
marker,
type: 'text',
value: [
{
htmlValue: value,
plainValue: value,
},
],
};
default:
return {
marker,
type,
value,
};
}
});
Result:
[
{
"marker": "first_name",
"type": "string",
"value": "Firstname"
},
{
"marker": "surname",
"type": "string",
"value": "Surname"
},
{
"marker": "email",
"type": "string",
"value": "your@email.com"
},
{
"marker": "topic",
"type": "list",
"value": [
{
"title": "Article",
"value": "article"
}
]
},
{
"marker": "text",
"type": "text",
"value": [
{
"htmlValue": "Some text",
"plainValue": "Some text"
}
]
}
]
6. post FormsData with FormData.postFormsData()
Example:
const formResponse = await FormData.postFormsData({
formIdentifier: 'contact_us',
formData: transformedFormData,
});
Result:
{
"formData": {
"formIdentifier": "contact_us",
"time": "2025-04-30T20:12:39.209Z",
"formData": [
{
"marker": "first_name",
"type": "string",
"value": "Firstname"
},
{
"marker": "surname",
"type": "string",
"value": "Surname"
},
{
"marker": "email",
"type": "string",
"value": "your@email.com"
},
{
"marker": "topic",
"type": "list",
"value": [
{
"title": "Article",
"value": "article"
}
]
},
{
"marker": "text",
"type": "text",
"value": [
{
"htmlValue": "Some text",
"plainValue": "Some text"
}
]
}
],
"id": 68
},
"actionMessage": "Message about successful data processing"
}
Final example
// 1. Import oneEntry and define PROJECT_URL and APP_TOKEN
import { defineOneEntry } from 'oneentry';
const PROJECT_URL = 'your-project-url';
const APP_TOKEN = 'your-app-token';
// 2. Creating an API client
const { FileUploading, FormData } = defineOneEntry(PROJECT_URL, {
token: APP_TOKEN,
});
// 3. Get form and sort fields by position
const formData = await Forms.getFormByMarker('contact_us');
const formFields = formData?.attributes.slice().sort(
(a: { position: number }, b: { position: number }) =>
a.position - b.position,
);
// 4. Get fields data from frontend Form
const fieldsData = [
{
marker: 'first_name',
value: 'Firstname',
},
{
marker: 'surname',
value: 'Surname',
},
{
marker: 'email',
value: 'your@email.com',
},
{
marker: 'topic',
value: [
{
title: 'Article',
value: 'article',
},
],
},
{
marker: 'text',
value: 'Some text',
},
];
// 5. Transform data for use with OneEntry Form
const transformedFormData = fieldsData.map((field: any) => {
const { marker, value } = field;
const formField = formFields.find(
(field: { marker: string }) => field.marker === marker,
);
const { type } = formField;
switch (type) {
case 'text':
return {
marker,
type: 'text',
value: [
{
htmlValue: value,
plainValue: value,
},
],
};
default:
return {
marker,
type,
value,
};
}
});
// 6. post FormsData with FormData.postFormsData()
const formResponse = await FormData.postFormsData({
formIdentifier: 'contact_us',
formData: transformedFormData,
});
console.log(formResponse);