Kin Form
Docs
INTRODUCTION
Getting Started Architecture

EXAMPLES
Basic Example Any-level Validation Dependent Field Validation Flexible Form Structures Flexible Field Components Array

API

Dependent Field Validation

This example demonstrates dependent field validation.

Key Concepts

A common requirement in forms is for one field's validation to depend on another field's value, such as a "confirm password" field. Kin Form supports this with two key features:

  1. The dependents Property: The password field has a dependents property set to ["confirmPassword"]. This tells the form that whenever the password field's value changes, it should automatically trigger a re-validation of the confirmPassword field. This ensures the validation logic is always run at the right time.

    <text-field
      dependents='["confirmPassword"]'
      label="Password"
      ${field("password")}
    ></text-field>
    
  2. Accessing the Parent: Inside the validator for the confirmPassword field, we can access the parent FieldGroup (in this case, the FormController itself) via the parent property on the field state. This allows the validator to get the value of the password field (parent.value.password) and compare it to the current field's value.

    const confirmPasswordValidators = [
      // ... other validators
      ({ value, parent }: TextField<Model>) =>
        value === parent!.value.password ? null : "Passwords don't match",
    ];
    

By using dependents to trigger validation and the parent property to access sibling field values, you can easily implement complex cross-field validation rules.

Live Demo

On this page

Key Concepts Live Demo
Created with ♥️ by  Man Hoang (Kin)