ValidationRule
A type alias for validation rule functions.
A validation rule is a function that takes a value of type V and returns a ValidationResult indicating whether the validation passed or failed. This is the fundamental building block for all validation logic in the form system.
The form system provides type-specific rule extensions in the soil.form.rule
package for common data types (String, Int, Double, Boolean, Array, Collection, etc.), which offer convenient DSL methods for building validation rules. For custom validation logic, you can create your own extension functions on the appropriate rule builder types.
Using type-specific extensions:
val stringRules = rules<String> {
notEmpty { "Value cannot be empty" }
minLength(3) { "Must be at least 3 characters" }
}
Content copied to clipboard
Creating custom validation extensions:
fun StringRuleBuilder.email(message: () -> String) {
val pattern = Regex("^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$")
extend(StringRule({ pattern.matches(this) }, message))
}
val emailRules = rules<String> {
notBlank { "Email is required" }
email { "Must be a valid email address" }
}
Content copied to clipboard
Parameters
V
The type of the value to be validated.