JSON definition · shared/definitions/load-definition-variables-data.json
View on GitHub
{
"title": "Insurance claim for {user.name} ({user.plan} plan)",
"description": "Tell us what happened; we usually answer within two working days.",
"completeText": "Submit claim",
"elements": [
{
"type": "text",
"name": "q_email",
"valueName": "customer_email",
"title": "Email",
"inputType": "email",
"isRequired": true
},
{
"type": "text",
"name": "q_amount",
"valueName": "amount",
"title": "Amount claimed",
"inputType": "number",
"min": 0
},
{
"type": "dropdown",
"name": "incident",
"title": "What happened?",
"choices": [
{
"value": "collision",
"text": "Collision"
},
{
"value": "theft",
"text": "Theft"
},
{
"value": "weather",
"text": "Storm or flood"
}
]
},
{
"type": "comment",
"name": "details",
"title": "Details"
},
{
"type": "boolean",
"name": "priority_handling",
"title": "Would you like priority handling?",
"description": "Available with your premium plan.",
"visibleIf": "{user.plan} = 'premium'"
}
]
}
Client · shared/client/load-definition-variables-data.js · sjs:load-definition-variables-data.client
View on GitHub
import { Model } from "survey-core";
async function createSurvey(recordId) {
if (window.FORM_DEFINITION) {
// Option A: Use the schema and user variables embedded in the HTML response
const survey = new Model(window.FORM_DEFINITION);
survey.setVariables(window.FORM_VARIABLES);
return survey;
}
// Option B: Load the definition, variables, and previous answers in one request
const { definition, variables, data } =
await fetch(`/api/forms/claim-request?record=${encodeURIComponent(recordId)}`).then(r => r.json());
const survey = new Model(definition);
survey.setVariables(variables); // Variables are available in expressions such as {user.name} and are excluded from survey.data
survey.data = data; // Restore previously saved answers for editing
return survey;
}
Server · src/main/java/io/surveyjs/examples/LoadDefinitionVariablesDataController.java · sjs:load-definition-variables-data.server
View on GitHub
// Option A: Embed the schema and user variables in the HTML response
@GetMapping("/claim")
public String claimPage(@AuthenticationPrincipal User user, Model model) throws IOException {
model.addAttribute("formDefinition", json.readTree(Files.readString(DEFINITION)));
model.addAttribute("formVariables", Variables.of(user));
// The template writes both with th:inline="javascript", which escapes them for an inline <script>
return pages.render("load-definition-variables-data", model.addAttribute("optionA", true));
}
// Option B: Return the definition, variables, and previous answers in one response
@GetMapping("/api/forms/{id}")
@ResponseBody
public FormAnswer loadForm(@PathVariable String id, @RequestParam(required = false) Long record, @AuthenticationPrincipal User user) {
String form = jdbc.sql("SELECT json FROM forms WHERE key = ?").param(id).query(String.class).optional()
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Unknown form"));
Optional<JsonNode> saved = jdbc.sql("SELECT data FROM responses WHERE id = ?" + (id.equals("claim-request") ? " AND form_id = 'claim'" : ""))
.param(record).query(String.class).optional().map(json::readTree);
if (id.equals("claim-request")) { // only the signed-in customer's own claims
// H2 can't query inside JSON text, so the owner check reads the document
saved = saved.filter(data -> user != null && user.email().equals(data.path("customer_email").asString(null)));
}
return new FormAnswer(json.readTree(form), Variables.of(user), saved.orElse(json.createObjectNode())); // {} without a record
}