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 · examples/views/load_definition_variables_data.py · sjs:load-definition-variables-data.server
View on GitHub
# Option A: Embed the schema and user variables in the HTML response
def claim_page(request: HttpRequest) -> HttpResponse:
definition = json.loads(DEFINITION.read_text(encoding="utf-8"))
user = request.user
variables = {"user": {"name": user.get_full_name(), "plan": user.plan} if user.is_authenticated else {"name": None, "plan": None}}
# The template writes both with json_script, which escapes them for the page
return render_example(request, "load-definition-variables-data", form_definition=definition, form_variables=variables)
# Option B: Return the definition, variables, and previous answers in one response
def get_form(request: HttpRequest, form_id: str) -> JsonResponse:
if (form := Form.objects.filter(key=form_id).first()) is None:
return JsonResponse({"error": "Unknown form"}, status=404)
user, record = request.user, request.GET.get("record", "")
saved = Response.objects.filter(id=record) if record.isdigit() else Response.objects.none()
if form_id == "claim-request": # only the signed-in customer's own claims
email = user.email if user.is_authenticated else ""
saved = saved.annotate(email=JsonExtract("data", "$.customer_email")).filter(form_id="claim", email=email)
answers = saved.values_list("data", flat=True).first()
return JsonResponse({
"definition": json.loads(form.json), # the stored form definition
"variables": {"user": {"name": user.get_full_name(), "plan": user.plan} if user.is_authenticated else {"name": None, "plan": None}},
"data": json.loads(answers) if answers else {}, # previous answers, or {} without them
})