Core concepts
The mental model. Survey programmers who skip this page tend to write fragile logic; those who read it once tend to write logic that holds up.
Two languages, two jobs
Fieldbase is built around a hard separation between what the survey contains and what happens when.
- YAML describes the survey’s structure: question ids, types, options, scales, rows, pages, blocks, loops.
- Python describes runtime behaviour: when to show a question, how to pipe a previous answer into text, when to fail validation, when to terminate.
Anything that can be authored once and never changes per respondent goes in YAML. Anything that depends on what the respondent has done goes in Python.
The four runtime objects
When the survey runs, every question becomes a proxy — a runtime object accessed by its qid. There are four runtime objects worth understanding up front; everything else builds on them.
Q — the question proxy
QBrand.value # the answer (scalar for single/open/number)
QBrand.selected # collection of selected codes (single/multi/rank)
QBrand.selectedLabel # display label (single)
QBrand.selectedLabels # collection of labels (multi/rank)
QBrand.answered # True if the respondent has answered
QBrand.matched # lookup-only: True if the input matched a record
Q is whatever qid you wrote. The accessors available depend on the
question type — see Question proxies for
the full list per type.
Collection — what .selected returns
QBrand.selected.any(2) # True if code 2 is selected
QBrand.selected.any([2, 3, 4]) # True if any of 2/3/4 is selected
QBrand.selected.every([2, 3]) # True if both 2 AND 3 are selected
QBrand.selected.size # how many selected
QBrand.selected.intersect([1, 2]) # collection of overlap codes
QBrand.selected.minus([5]) # codes minus the listed ones
QBrand.selected.union(QOther.selected) # combined codes
Collections are what make routing readable. They’re set-like, so order doesn’t affect equality, and they compose. See Collections for the full method reference.
Row / cell — for grids and lists
QGrid.row(2).selected.any(4) # row 2's selection in a gridSingle/Multi
QGrid.row(2).value # row 2's value (numberList, openList)
QGrid.row(2).col(1) # specific cell in gridNumber
QGrid.row(2).col(1).value # the cell's number
Grids and lists store one answer per row (and per cell, for
gridNumber). .row(N) resolves to a row object whose accessors are
the same shape as a top-level question proxy. See
Row & cell access.
Loop — the iteration proxy
BrandLoop.value # the current iteration's code
BrandLoop.selectedLabel # the current iteration's label
BrandLoop.all # codes the loop will iterate over
BrandLoop.size # how many iterations
A loop walks through a set of codes (usually from a previous question’s
.selected) and re-renders its body for each. See Loops.
Three ways a domain can change
A “domain” is the set of options, rows, or scale items a question shows. Three things can affect what a respondent actually sees:
- Authored — the YAML’s
options,group, orscaledefine the full domain. - Masked —
options.fromorscale.fromfilters the authored domain at runtime. Masked items still exist in the model; they just don’t render. - Skipped — an
ifon the question itself can hide the entire question, in which case nothing about its domain matters.
The export reflects the difference. A masked-out option exports as
null; a not-selected option exports as 0; a selected option exports
as 1. See Tri-state projection for
why this matters in analysis.
answered vs selected vs matched vs value
These accessors look interchangeable and are easy to confuse:
| Accessor | Meaning |
|---|---|
Q.answered | The respondent has interacted with this question and it has a stored answer. Available on every type. |
Q.selected | A collection of selected codes — single, multi, rank. Empty collection if not answered. |
Q.value | The scalar answer — number, open text, or single code as a string. None if not answered. |
Q.matched | lookup-only: did the respondent’s input match a record in the lookup source. |
When unsure, Q.answered is the safest gate for “has the respondent
got this far”.
Loop iteration vs loop answer
A subtle distinction worth pinning early:
BrandLoop(the loop proxy) refers to the currently iterating code.BrandLoop.valueis the iteration code, not an answer.- Questions inside the loop have their own qids and store one answer
per iteration. Their export columns get a
_Nsuffix (see Loop suffixes).
So if QRating lives inside BrandLoop, the export contains
QRating_1, QRating_2, QRating_3 — one per iteration of the loop.
Reading the YAML the way the parser does
Two conventions matter for any non-trivial example:
- Four-space indent under
- qid:,- script:,- if:,- loop:, etc. Two-space indent silently breaks the parse. - Coded options use numeric keys, not strings.
1: Appleis right;"1": Appleis wrong;apple: Appleis wrong.
Beyond those two, the YAML is conventional.
See also
- Question proxies — every accessor by question type.
- Collections — full method reference.
- Loops — iteration mechanics.
- Tri-state projection — why
nullmatters. - Glossary — every term defined.