Skip to content

Binder: Cross-Field validation #23298

@tltv

Description

@tltv

Field-level validator callbacks should be run inside a signal effect so that changes to any signal used by the validator will be detected and trigger running the validator again. Practical cross-field validation furthermore requires that the value of other bindings are available as signals, by adding a signal-aware value getter to Binding.

var passwordBinding = binder.bind(passwordField, "password");

binder.forField(confirmPasswordField)
  .withValidator(value -> !useStrictValidationSignal.value() || Objects.equals(value, passwordBinding.value()), 
    "Must match the password")
  .bind("confirmPassword");

This can be implemented by running the validator callback and updating the binding's validation status inside a signal effect. The effect can be a ComponentEffect that is bound to the field used for the binding as long as the field is actually also a component (i.e. not just a plain HasValue).

[subtask] For cases where a field component is not available, including binder-level validators, we should instead run the callback with a custom signal dependency tracker that throws an error if any signal value is read to make the developer aware that there's unexpectedly no effect in that particular case. Support for the cases without a field component would require handling all validation status changes as computed signals instead of as effects. Refactoring Binder to work in that way is outside the scope of this ticket.

Sub-issues

Metadata

Metadata

Assignees

Labels

Projects

Status

🔎Iteration reviews

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions