Implement a React Formik form with useFormConfig hook.
In this example, we demonstrate how to implement a React Formik form using the OneEntry useFormConfig hook.
✅ Purpose of the scenario:
- Implement a Formik form utilizing the OneEntry useFormConfig hook.
- Ensure data is correctly formatted and validated before submission.
✅ What you need:
- A valid PROJECT_URL and APP_TOKEN for authentication with the OneEntry API.
- A development environment set up with React.js enabled (e.g., Next.js, Create React App).
- Configured in the OneEntry UI contact form with the "contact_us" marker.
📌 Important:
- These examples do not include error handling.
- You can manage errors using a try-catch block or by employing a construction like await Promise.catch((error) => error).
- Ensure that the form marker (e.g., "contact_us") used in Forms.getFormByMarker matches an existing form in your OneEntry project.
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 { Forms } = defineOneEntry(PROJECT_URL, {
token: APP_TOKEN,
});
Get form data from oneentry
Example:
const formData = await Forms.getFormByMarker('contact_us');
Result:
{
"id": 3,
"attributeSetId": 2,
"type": "data",
"localizeInfos": {
"title": "Contact us",
"titleForSite": "",
"successMessage": "Message about successful data processing",
"unsuccessMessage": "Message about unsuccessful data processing",
"urlAddress": "",
"database": "0",
"script": "0"
},
"version": 5,
"position": 1,
"identifier": "contact_us",
"processingType": "script",
"templateId": null,
"attributes": [
{
"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
}
]
}
Use hook useFormConfig
This hook provides form configuration data.
Example:
const {
fields,
initialValues,
validationSchema,
} = useFormConfig(formData?.attributes);
Formik integration
Example:
<Formik initialValues={initialValues} validationSchema={validationSchema} onSubmit={() => {}}>
{({
values,
errors,
touched,
handleChange,
handleBlur,
handleSubmit,
isSubmitting,
}) => {
return (
<form onSubmit={handleSubmit}>
<input
type="email"
name="email"
onChange={handleChange}
onBlur={handleBlur}
value={values?.email}
/>
<input
type="text"
name="first_name"
onChange={handleChange}
onBlur={handleBlur}
value={values?.first_name}
/>
<input
type="text"
name="surname"
onChange={handleChange}
onBlur={handleBlur}
value={values?.surname}
/>
<button type="submit" disabled={isSubmitting}>
Submit
</button>
</form>
);
}}
</Formik>
Final example
// 1. Import defineOneEntry, Formik and useFormConfig
import { defineOneEntry } from 'oneentry';
import { Formik } from 'formik';
import useFormConfig from './useFormConfig';
// 1.1 define PROJECT_URL and APP_TOKEN
const PROJECT_URL = 'your-project-url';
const APP_TOKEN = 'your-app-token';
// 2. Creating an API client
const { Forms, FormData } = defineOneEntry(PROJECT_URL, {
token: APP_TOKEN,
});
// 3. Get form data from OneEntry
const formData = await Forms.getFormByMarker('contact_us');
// 4. Extract form configuration with useFormConfig react hook
const {
fields,
initialValues,
validationSchema,
} = useFormConfig(formData?.attributes);
// 5. Create form with Formik and useFormConfig data
return (
<Formik initialValues={initialValues} validationSchema={validationSchema} onSubmit={() => {}}>
{({
values,
errors,
touched,
handleChange,
handleBlur,
handleSubmit,
isSubmitting,
}) => {
return (
<form onSubmit={handleSubmit}>
<input
type="email"
name="email"
onChange={handleChange}
onBlur={handleBlur}
value={values?.email}
/>
<input
type="text"
name="first_name"
onChange={handleChange}
onBlur={handleBlur}
value={values?.first_name}
/>
<input
type="text"
name="surname"
onChange={handleChange}
onBlur={handleBlur}
value={values?.surname}
/>
<button type="submit" disabled={isSubmitting}>
Submit
</button>
</form>
);
}}
</Formik>
);