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:
-
The
dependents
Property: Thepassword
field has adependents
property set to["confirmPassword"]
. This tells the form that whenever thepassword
field's value changes, it should automatically trigger a re-validation of theconfirmPassword
field. This ensures the validation logic is always run at the right time.<text-field dependents='["confirmPassword"]' label="Password" ${field("password")} ></text-field>
-
Accessing the Parent: Inside the validator for the
confirmPassword
field, we can access the parentFieldGroup
(in this case, theFormController
itself) via theparent
property on the field state. This allows the validator to get the value of thepassword
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.