Skip to content

FormCraftField Class

FormCraftField<Value, ValidatedValue> describes one form field. It stores raw value, validation state, and async validation rule.

This type is typically used inside a FormCraftFields schema.

Constructor

swift
init(
    value: Value,
    delayValidation: FormCraftDelayValidation = .immediate,
    rule: @escaping (_ value: Value) async -> FormCraftValidationResponse<ValidatedValue>
)

Arguments

  • value: Value - the current value of the field.
  • delayValidation: FormCraftDelayValidation - delay before field validation starts (default is .immediate).
  • rule - async validation rule that returns FormCraftValidationResponse<ValidatedValue>.

Example:

swift
var email = FormCraftField(value: "") { value in
    await FormCraftValidationRules()
        .string()
        .trimmed()
        .notEmpty()
        .email()
        .validate(value: value)
}

Properties

PropertyTypeDefaultDescription
valueValuevalue passed to initCurrent field value.
validatedValueValidatedValue?nilLast validated value produced by a successful validation.
defaultValueValuesame as initial valueInitial value used to determine whether the field was changed.
mountedBoolfalseWhether this field is currently mounted in UI.
errorsFormCraftFailure?nilCurrent validation errors for the field.
isValidationBoolfalseWhether validation is currently in progress.
taskValidationTask<Void, Never>?nilReference to the active validation task.
isDirtyBoolfalseIndicates whether the current value has changed from defaultValue.
isErrorBoolcomputedConvenience flag that mirrors errors != nil.
delayValidationFormCraftDelayValidation.immediateDelay policy used before validation starts.
rule(Value) async -> FormCraftValidationResponse<ValidatedValue>requiredValidation function for the field value.

Methods

validate

swift
func validate() async -> FormCraftFailure?

Runs the field rule with current value.

  • Returns nil when validation succeeds.
  • Returns FormCraftFailure when validation fails.

Released under the MIT License.