fieldState
fieldState returns one field’s value and the error to show.
const { value, error, invalid } = fieldState(state, name, options?)Reference
Section titled “Reference”fieldState(state, name, options?)
Section titled “fieldState(state, name, options?)”Parameters
Section titled “Parameters”state: aSessionState.name: the question name.options.reveal(optional): run client checks. Defaultfalse. Your view decides when: on blur, or a Continue attempt.options.messages(optional): copy overrides. See messages.
Returns
Section titled “Returns”A FieldState: { value, error, invalid }.
Caveats
Section titled “Caveats”- A server error wins and suppresses client checks.
- An unknown name is empty and valid.
sameFieldState(a, b)
Section titled “sameFieldState(a, b)”Deep equality. Pass it as session.select’s equals, so a field re-renders only when its own state changes.
validate(field, value, messages?)
Section titled “validate(field, value, messages?)”The client checks on their own. Returns a message or undefined.
Order: required, then length, email, US phone, zip, number (integer, min, max), date bounds and choice membership. The message is the question’s invalidMessage, else the default copy. info never fails.
serverError(errors, field, messages?)
Section titled “serverError(errors, field, messages?)”One field’s server error: { message, invalid }. When an error spans several fields, the first gets the message; the others are marked invalid.
Messages
Section titled “Messages”MESSAGES: the default English copy.resolveMessages(overrides): merge your own copy over the defaults. This is the i18n hook.formatMessage(template, { min, max }): fill{min}and{max}.
Per-field subscription
Section titled “Per-field subscription”session.select( (s) => fieldState(s, name, { reveal: touched.has(name) }), ({ error, invalid }) => updateField(name, error, invalid), sameFieldState,);Your own copy
Section titled “Your own copy”const messages = resolveMessages({ required: 'This one is required' });fieldState(state, name, { reveal: true, messages });