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
dependentsProperty: Thepasswordfield has adependentsproperty set to["confirmPassword"]. This tells the form that whenever thepasswordfield's value changes, it should automatically trigger a re-validation of theconfirmPasswordfield. 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
confirmPasswordfield, we can access the parentFieldGroup(in this case, theFormControlleritself) via theparentproperty on the field state. This allows the validator to get the value of thepasswordfield (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.