Documentation Index
Fetch the complete documentation index at: https://docs.joyfill.io/llms.txt
Use this file to discover all available pages before exploring further.
This guide shows you how to validate required fields in Joyfill forms.
How It Works
Joyfill automatically validates required fields based on:
- Field requirement: Fields marked as required = true
- Field visibility: Hidden fields are always filtered out of the validation Output
- Field values: Required fields must have non-empty values to be valid
Document-Level Validation
Validate all fields in your document:
val editor = rememberDocumentEditor(json)
// Validate entire document
val validity = editor.validate()
when (validity) {
is FieldsValid -> {
// All required fields have values
println("All fields are valid!")
}
is FieldsInvalid -> {
// Handle validation errors
println("Some fields are invalid")
}
}
Handling Invalid Fields
When some required fields are empty:
when (val validity = editor.validate()) {
is FieldsInvalid -> {
// Access valid fields directly
validity.validFields.forEach { field ->
println("${field.component.title} is valid")
}
// Access invalid fields directly
validity.invalidFields.forEach { field ->
println("${field.component.title} is invalid")
field.messages.forEach { message ->
println("Error: $message")
}
}
}
}
Individual Field Validation
Validate specific fields:
// Get a field editor
val nameField = editor.fields.text("Full Name")
// Validate the field
val fieldValidity = nameField?.validate()
when (fieldValidity) {
is ComponentValid -> {
println("Field is valid")
}
is ComponentInvalid -> {
println("Field is invalid:")
fieldValidity.messages.forEach { message ->
println("- $message")
}
}
}
Validation Messages
Required field validation provides standard error messages:
when (val validity = editor.validate()) {
is FieldsInvalid -> {
validity.invalidFields.forEach { field ->
// Standard format: "Component [title] is required"
println(field.messages.first()) // "Component Full Name is required"
}
}
}
Checking Field Requirements
Check if a field is required:
val nameField = editor.fields.text("Full Name")
val isRequired = nameField?.component?.required == true
if (isRequired) {
println("This field is required")
}
Validation Rules
| Field Type | Condition | Valid? |
|---|
| Required | Has non-empty value | Yes |
| Required | Empty or null | No |
| Required | Hidden | Yes (always) |
| Optional | Any value | Yes (always) |
Key Points
- Required fields are automatically validated based on the
required property
- Hidden fields are always filtered out of the validation Output
- Validation errors use the format: “Component [title] is required”
- Call
editor.validate() anytime to get current validation status
- Document validation checks all visible required fields at once