SurveyJS PHP Backend Integration with Laravel /

Variable Presets

Variable Presets

Use when: Authors need to test form logic with sample variable values in the Preview tab.

Read this step in the docs

Try this

  1. In Preview, switch between the basic and premium presets: "Call me back within one hour" appears only for premium (visibleIf: {customerTier} = 'premium').
  2. Edit a preset (change a value or add one): Requests shows one PUT /api/variable-presets/support. Just switching presets sends nothing.
  3. Reload the page: your edit is still there.
  4. Switch the Demo user to Bob: editing a preset answers 403 from the same Gate::authorize('edit-forms').

Commercial client licence: without SURVEYJS_LICENSE_KEY the component shows a licence banner.

    No requests yet.

      What the server stored

      Source on GitHub Definition: shared/definitions/variable-presets.json Commercial client licence: without SURVEYJS_LICENSE_KEY the component shows a licence banner

      Client · 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)
          });
        }
      });
      Server · 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();
      });