تقديم نموذج مع مرفق ملف
في هذا المثال، نوضح كيفية تقديم نموذج يتضمن مرفق ملف باستخدام واجهة برمجة التطبيقات OneEntry.
✅ هدف السيناريو:
- الحصول على تكوين النموذج من منصة OneEntry
- يقوم المستخدم بملء نموذج (مثل الرد، التعليقات).
- إرفاق ملف (صورة، سيرة ذاتية، مستند).
- يتم تخزين البيانات والملف معًا في FormData
- تقديم البيانات المجمعة إلى واجهة برمجة التطبيقات OneEntry.
✅ ما تحتاجه:
- عنوان PROJECT_URL و APP_TOKEN صالحين للمصادقة مع واجهة برمجة التطبيقات OneEntry.
- نموذج في OneEntry يحتوي على حقل من نوع "ملف" (type file)
- علامة النموذج (مثل resume_form)
📌 مهم:
- هذه الأمثلة لا تتضمن معالجة الأخطاء.
- يمكنك إدارة الأخطاء باستخدام كتلة try-catch أو من خلال استخدام بناء مثل await Promise.catch((error) => error).
السيناريو
1. استيراد defineOneEntry من SDK وتعريف url و token
مثال:
import { defineOneEntry } from 'oneentry';
const PROJECT_URL = 'your-project-url';
const APP_TOKEN = 'your-app-token';
2. إنشاء عميل API باستخدام دالة defineOneEntry()
مثال:
const { Forms, FormData } = defineOneEntry(PROJECT_URL, {
token: APP_TOKEN,
});
3. الحصول على النموذج بواسطة العلامة من OneEntry
مثال:
const fileForm = await Forms.getFormByMarker('file');
النتيجة:
{
"id": 8,
"attributeSetId": 11,
"type": "data",
"localizeInfos": {
"title": "File",
"titleForSite": "",
"successMessage": "Successful data processing",
"unsuccessMessage": "Unsuccessful data processing",
"urlAddress": "",
"database": "0",
"script": "0"
},
"version": 0,
"position": 1,
"identifier": "file",
"processingType": "script",
"templateId": null,
"attributes": [
{
"type": "file",
"marker": "file",
"isLogin": null,
"isSignUp": null,
"position": 1,
"settings": {},
"isVisible": true,
"listTitles": [],
"validators": {},
"localizeInfos": {
"title": "File"
},
"additionalFields": [],
"isNotificationEmail": null,
"isNotificationPhoneSMS": null,
"isNotificationPhonePush": null
},
{
"type": "image",
"marker": "image",
"isLogin": null,
"isSignUp": null,
"position": 2,
"settings": {},
"isVisible": true,
"listTitles": [],
"validators": {},
"localizeInfos": {
"title": "Image"
},
"additionalFields": [],
"isNotificationEmail": null,
"isNotificationPhoneSMS": null,
"isNotificationPhonePush": null
},
{
"type": "groupOfImages",
"marker": "images_group",
"isLogin": null,
"isSignUp": null,
"position": 3,
"settings": {},
"isVisible": true,
"listTitles": [],
"validators": {},
"localizeInfos": {
"title": "Images group"
},
"additionalFields": [],
"isNotificationEmail": null,
"isNotificationPhoneSMS": null,
"isNotificationPhonePush": null
}
],
"moduleFormConfigs": [
{
"id": 3,
"moduleIdentifier": "content",
"isGlobal": false,
"isClosed": false,
"viewOnlyUserData": false,
"commentOnlyUserData": false,
"entityIdentifiers": [
{
"id": "services",
"isNested": false
}
]
}
]
}
4. الحصول على إعدادات إضافية لتقديم النموذج
مثال:
const moduleFormConfig = fileForm.moduleFormConfigs[0];
5. رفع ملف مع إدخال النموذج=input=file
إرسال
البيانات:
// JS
let selectedFiles = null;
// handleFileChange
const handleFileChange = (e: any) => {
selectedFiles = e.target.files;
};
// handleSubmit
const handleSubmit = async (e, file) => {
e.preventDefault();
// 4. Get additional settings for the form submission
const moduleFormConfig = fileForm?.moduleFormConfigs?.[0];
// 5. Preparing form data
const formData = [
{
marker: 'file',
type: 'file',
value: file,
fileQuery: {
type: 'page',
entity: 'editor',
id: 4965,
},
},
];
// 6. Submitting a form with FormData.postFormsData()
const postFormResp = await FormData.postFormsData({
formIdentifier: 'file',
formData: formData,
formModuleConfigId: moduleFormConfig.id,
moduleEntityIdentifier: moduleFormConfig.entityIdentifiers[0]id,
replayTo: null,
status: 'sent',
});
};
مثال:
// HTML
<form
onSubmit={(e) => handleSubmit(e, selectedFiles)}
className="flex flex-col gap-3 p-10 border rounded-3xl"
>
<input
type="file"
onChange={handleFileChange}
className="bg-white text-slate-800 p-3 rounded-2xl"
/>
<button
type="submit"
className="bg-white text-slate-800 p-3 rounded-2xl"
>
Send
</button>
</form>
النتيجة:
{
"formData": {
"formIdentifier": "file",
"time": "2025-11-10T06:30:56.488Z",
"formData": [
{
"marker": "file",
"type": "file",
"value": [
{
"filename": "files/project/page/4965/editor/4f82c7e0-b1c5-4144-b7dc-202e3b50d357.json",
"downloadLink": "https://your-project.oneentry.cloud/cloud-static/files/project/page/4965/editor/4f82c7e0-b1c5-4144-b7dc-202e3b50d357.json",
"size": 259391
}
]
}
],
"entityIdentifier": "services",
"fingerprint": "UQ_rrofe2",
"isUserAdmin": false,
"formModuleId": 3,
"userIdentifier": null,
"parentId": null,
"id": 119
},
"actionMessage": "Successful data processing"
}
المثال النهائي
// 1. Import defineOneEntry from SDK 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 { Forms, FormData } = defineOneEntry(PROJECT_URL, {
token: APP_TOKEN,
});
// 3. Get form by marker from OneEntry
const fileForm = await Forms.getFormByMarker('file');
// 3.1 Uploading a file with form input=file
// JS
const selectedFiles = [];
const handleFileChange = (e) => {
selectedFiles = e.target.files;
};
// handleSubmit
const handleSubmit = async (e, file) => {
e.preventDefault();
// 4. Get additional settings for the form submission
const moduleFormConfig = fileForm.moduleFormConfigs[0];
// 5. Preparing form data
const formData = [
{
marker: 'file',
type: 'file',
value: file,
fileQuery: {
type: 'page',
entity: 'editor',
id: 4965,
},
},
];
// 6. Submitting a form with FormData.postFormsData()
const postFormResp = await FormData.postFormsData({
formIdentifier: 'file',
formData: formData,
formModuleConfigId: moduleFormConfig.id,
moduleEntityIdentifier: moduleFormConfig.entityIdentifiers[0].id,
replayTo: null,
status: 'sent',
});
};
// HTML
<form
onSubmit={(e) => handleSubmit(e, selectedFiles)}
className="flex flex-col gap-3 p-10 border rounded-3xl"
>
<input
type="file"
onChange={handleFileChange}
className="bg-white text-slate-800 p-3 rounded-2xl"
/>
<button
type="submit"
className="bg-white text-slate-800 p-3 rounded-2xl"
>
Send
</button>
</form>