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 · Endpoints/LoadDefinitionVariablesData.cs · sjs:load-definition-variables-data.server
View on GitHub
// Option B: Return the definition, variables, and previous answers in one response
app.MapGet("/api/forms/{id}", async (string id, string? record, ClaimsPrincipal user, SurveyDb db) =>
{
var definition = await db.Forms.Where(f => f.Key == id).Select(f => f.Json).SingleOrDefaultAsync();
if (definition is null) return Results.Json(new { error = "Unknown form" }, statusCode: 404);
var email = user.FindFirstValue(ClaimTypes.Email);
long? recordId = long.TryParse(record, out var parsed) ? parsed : null; // ?record= is empty for a new response
var records = db.Responses.Where(r => r.Id == recordId);
if (id == "claim-request") // Only the signed-in customer's own claims
records = records.Where(r => r.FormId == "claim" && SurveyDb.JsonValue(r.Data, "$.customer_email") == email);
var data = recordId is null ? null : await records.Select(r => r.Data).SingleOrDefaultAsync();
return Results.Json(new
{
definition = JsonNode.Parse(definition), // Return the stored form definition
variables = new { user = new { name = user.Identity?.Name, plan = user.FindFirstValue("plan") } },
data = data is null ? new JsonObject() : JsonNode.Parse(data), // Return previous answers, or {} when none are available
});
});
Server · Pages/Examples/LoadDefinitionVariablesData.cshtml.cs · sjs:load-definition-variables-data.embed
View on GitHub
// Option A: Embed the schema and user variables in the HTML response
private void Embed()
{
var path = Path.Combine(env.ContentRootPath, "shared", "definitions", "load-definition-variables-data.json");
Definition = JsonNode.Parse(System.IO.File.ReadAllText(path));
Variables = new { user = new { name = User.Identity?.Name, plan = User.FindFirstValue("plan") } };
} // The page writes both with @Json.Serialize(), which escapes them for an inline script