انتقل إلى المحتوى الرئيسي

تقديم نموذج مع مرفق ملف

في هذا المثال، نوضح كيفية تقديم نموذج يتضمن مرفق ملف باستخدام واجهة برمجة التطبيقات OneEntry.

✅ هدف السيناريو:

  • الحصول على تكوين النموذج من منصة OneEntry
  • يقوم المستخدم بملء نموذج (مثل الرد، التعليقات).
  • يرفق ملفًا (صورة، سيرة ذاتية، مستند).
  • يتم تخزين البيانات والملف معًا في FormData
  • تقديم البيانات المجمعة إلى واجهة برمجة التطبيقات OneEntry.

✅ ما تحتاجه:

  • عنوان PROJECT_URL و APP_TOKEN صالحين للمصادقة مع واجهة برمجة التطبيقات OneEntry.
  • نموذج في OneEntry يحتوي على حقل من نوع "ملف" (type file)
  • علامة النموذج (مثل resume_form)

📌 مهم:

  • نحن لا نتعامل مع الأخطاء في هذه الأمثلة.
  • يمكنك التعامل مع الأخطاء في trycatch أو في بناء مثل "await Promise.catch((error) => error)"

السيناريو

1. استيراد oneEntry وتحديد 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,
});

مثال:

const fileForm = await Forms.getFormByMarker('file');
النتيجة:
{
"id": 8,
"attributeSetId": 11,
"type": "data",
"localizeInfos": {
"title": "ملف",
"titleForSite": "",
"successMessage": "تمت معالجة البيانات بنجاح",
"unsuccessMessage": "فشلت معالجة البيانات",
"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": "ملف"
},
"additionalFields": [],
"isNotificationEmail": null,
"isNotificationPhoneSMS": null,
"isNotificationPhonePush": null
},
{
"type": "image",
"marker": "image",
"isLogin": null,
"isSignUp": null,
"position": 2,
"settings": {},
"isVisible": true,
"listTitles": [],
"validators": {},
"localizeInfos": {
"title": "صورة"
},
"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": "مجموعة صور"
},
"additionalFields": [],
"isNotificationEmail": null,
"isNotificationPhoneSMS": null,
"isNotificationPhonePush": null
}
]
}

3. رفع ملف مع إدخال النموذج input=file

إرسال

البيانات:

// JS
let selectedFiles = null;

// handleFileChange
const handleFileChange = (e: any) => {
selectedFiles = e.target.files;
};

// handleSubmit
const handleSubmit = async (e, file) => {
e.preventDefault();

// 4. إعداد بيانات النموذج
const body = {
formIdentifier: 'file',
formData: [
{
marker: 'file',
type: 'file',
value: file,
fileQuery: {
type: 'page',
entity: 'editor',
id: 4965,
},
},
],
};

// 5. تقديم نموذج مع FormData.postFormsData()
const postFormResp = await FormData.postFormsData(body);
};

مثال:

// 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"
>
إرسال
</button>
</form>
النتيجة:
{
"formData": {
"formIdentifier": "file",
"time": "2025-08-29T17:31:42.196Z",
"formData": [
{
"marker": "file",
"type": "file",
"value": [
{
"filename": "files/project/page/4965/editor/live-adv-1-1756488701913.svg",
"downloadLink": "https://test-data.oneentry.cloud/cloud-static/files/project/page/4965/editor/live-adv-1-1756488701913.svg",
"size": 6131
}
]
}
],
"id": 96
},
"actionMessage": "تمت معالجة البيانات بنجاح"
}

المثال النهائي

// 1. استيراد oneEntry وتحديد PROJECT_URL و APP_TOKEN
import { defineOneEntry } from 'oneentry';

const PROJECT_URL = 'your-project-url';
const APP_TOKEN = 'your-app-token';

// 2. إنشاء عميل API
const { Forms, FormData } = defineOneEntry(PROJECT_URL, {
token: APP_TOKEN,
});

const fileForm = await Forms.getFormByMarker('file');

// 3. رفع ملف مع إدخال النموذج input=file
// JS
const selectedFiles = [];
const handleFileChange = (e) => {
selectedFiles = e.target.files;
};

// handleSubmit
const handleSubmit = async (e, file) => {
e.preventDefault();

// 4. إعداد بيانات النموذج
const body = {
formIdentifier: 'file',
formData: [
{
marker: 'file',
type: 'file',
value: file,
fileQuery: {
type: 'page',
entity: 'editor',
id: 4965,
},
},
],
};

// 5. تقديم نموذج مع FormData.postFormsData()
const postFormResp = await FormData.postFormsData(body);
};

// 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"
>
إرسال
</button>
</form>