JSON definition · shared/definitions/creator-uploads.json
View on GitHub
{
"title": "Customer support",
"description": "Tell us how we can help",
"elements": [
{
"type": "image",
"name": "help_image",
"imageLink": "/shared/samples/support-help.svg",
"altText": "Support resources",
"imageHeight": "180",
"imageWidth": "100%"
},
{
"type": "dropdown",
"name": "product",
"title": "Product",
"choices": [
"Form Library",
"Survey Creator",
"Dashboard",
"PDF Generator"
]
},
{
"type": "comment",
"name": "issue",
"title": "How can we help?",
"isRequired": true
}
]
}
Client · shared/client/creator-uploads.js · sjs:creator-uploads.client
View on GitHub
// Store each uploaded image separately and keep its URL in the form schema
creator.onUploadFile.add(async (_, options) => {
const label = options.propertyName === "logo" || options.elementType === "header" ? "Logo" : "Image";
uploads++; updateControls();
showError("");
try {
if (!options.files.length || options.files.some(file =>
file.size > 5242880 || !["image/png", "image/jpeg", "image/webp", "image/gif"].includes(file.type)))
throw new Error("Choose a PNG, JPEG, WebP, or GIF image up to 5 MB.");
const body = new FormData();
options.files.forEach(file => body.append("files[]", file));
const response = await fetch("/api/files", { method: "POST", body });
const ids = response.ok ? await response.json() : null;
if (!Array.isArray(ids) || ids.length !== options.files.length ||
ids.some(id => typeof id !== "string" || !id))
throw new Error(`Couldn't upload the ${label.toLowerCase()}. Please try again.`);
options.callback("success", `/api/files/${encodeURIComponent(ids[0])}`);
note(`${label} uploaded.`, "ok");
} catch (error) {
options.callback("error");
showError(error.message);
note(error.message, "error");
} finally { uploads--; updateControls(); }
});
Server · routes/examples/store-files.php · sjs:store-files.server
View on GitHub
// POST /api/files — store uploads, return their ids in the same order
Route::post('/api/files', function (Request $request) {
$validation = Validator::make($request->all(), [
'files' => 'required|array|min:1',
'files.*' => 'required|file|max:5120|mimetypes:'
.'image/png,image/jpeg,image/webp,image/gif,application/pdf',
]);
abort_if($validation->fails(), 422, $validation->errors()->first());
$ids = [];
// Field "files[]": PHP keeps only the last of several "files"
foreach ($request->file('files') as $file) {
abort_unless($file->isValid(), 400, $file->getErrorMessage());
$id = (string) Str::uuid();
// The "uploads" disk: local, S3…; named by id, never by the client
$file->storeAs('', $id, 'uploads');
DB::table('files')->insert([
'id' => $id, 'name' => $file->getClientOriginalName(), 'type' => $file->getMimeType(),
]);
$ids[] = $id;
}
return response()->json($ids);
});
Server · routes/examples/creator-load-save.php · sjs:creator-load-save.server
View on GitHub
// PUT /api/forms/{id} — store the definition as-is: it's one JSON document
Route::put('/api/forms/{id}', function (Request $request, string $id) {
Gate::authorize('edit-forms'); // editing is an admin action: 403 otherwise
// The raw body, not $request->input(): PHP arrays would turn {} into [] in the stored JSON
$definition = json_decode($request->getContent(), flags: JSON_THROW_ON_ERROR);
StoredJson::put('forms', $id, $definition); // stored as it arrived: 1.0 stays 1.0
return response()->noContent(); // the next page load renders the new version
});