Source on GitHub
Definition: shared/definitions/variable-presets.json
Commercial client licence: without SURVEYJS_LICENSE_KEY the component shows a licence banner
shared/client/variable-presets.js · sjs:variable-presets.client
View on GitHub
import { SurveyCreator } from "survey-creator-js";
const creator = new SurveyCreator({
variablePresets: {
definition: { // describes the variables and how authors edit them
elements: [{
type: "dropdown", name: "customerTier", title: "Customer plan",
choices: ["basic", "premium"]
}]
},
presets: await fetch(`/api/variable-presets/${formId}`).then(r => (r.ok ? r.json() : []))
}
});
// Save the presets when an author edits them
creator.onVariablePresetsChanged.add((_, options) => {
if (options.reason === "edit") {
fetch(`/api/variable-presets/${formId}`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(options.variablePresets)
});
}
});
routes/examples/variable-presets.php · sjs:variable-presets.server
View on GitHub
// GET /api/variable-presets/{formId} — return the stored variable presets (404 if none yet)
Route::get('/api/variable-presets/{formId}', function (string $formId) {
$json = DB::table('variable_presets')->where('key', $formId)->value('json') ?? abort(404, 'No presets saved yet');
return response($json)->header('Content-Type', 'application/json');
});
// PUT /api/variable-presets/{formId} — store the variable presets as-is: it's one JSON document
Route::put('/api/variable-presets/{formId}', function (Request $request, string $formId) {
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
$presets = json_decode($request->getContent(), flags: JSON_THROW_ON_ERROR);
StoredJson::put('variable_presets', $formId, $presets); // stored as it arrived: 1.0 stays 1.0
return response()->noContent();
});