dependsOn method

RuleFor<T> dependsOn(
  1. String anotherFieldName
)

Declares that this rule depends on another control identified by anotherFieldName.

Whenever the dependency's value changes, the target field is "refreshed", forcing re-validation and UI updates (useful for cross-field validation).

Notes:

  • Dependencies are de-duplicated by _deps to prevent multiple listeners.
  • Listeners are tracked and removed when FormRules.dispose is called.

Implementation

RuleFor<T> dependsOn(String anotherFieldName) {
  // Avoid duplicated subscriptions for the same dependency.
  if (!_deps.add(anotherFieldName)) return this;

  // Listen to the dependency's value changes.
  final dep = _rules.form.control<dynamic>(anotherFieldName);

  // When the dependency changes, revalidate/refresh the target field.
  void handler() => _refreshTarget();

  dep.valueNotifier.addListener(handler);

  // Track disposer to remove the listener later.
  _rules._track(() => dep.valueNotifier.removeListener(handler));

  // Optionally refresh once immediately so the target state is consistent
  // with the current dependency values.
  _refreshTarget();

  return this;
}