updateValue<T extends FormFieldState<G>, G> method

void updateValue<T extends FormFieldState<G>, G>(
  1. String fieldName,
  2. G value
)

Updates the field value and status.

This method updates the value and status of a specific form field.

  • fieldName The name of the field to update.
  • value The new value for the field.

Implementation

void updateValue<T extends FormFieldState<G>, G>(String fieldName, G value) {
  var field = state.get<T, G>(fieldName);

  var updatedField = value != null
      ? field.copyWith(value: value)
      : field.copyWithNullable(value: null);

  final error = updatedField.validate(state.fields);

  if (error == null) {
    updatedField = updatedField.updateError(null);
  } else if (updatedField.touched && updatedField.validateOnUpdate) {
    updatedField = updatedField.updateError(error);
  }
  state.fields[fieldName] = updatedField;

  var status = state.status;

  // Downgrade status if it was previously submitted or failed
  if (state.status == BondFormStateStatus.submitted ||
      state.status == BondFormStateStatus.failed) {
    status = error != null
        ? BondFormStateStatus.invalid
        : _allValid(false)
            ? BondFormStateStatus.valid
            : BondFormStateStatus.invalid;
  } else if (error != null) {
    status = BondFormStateStatus.invalid;
  } else if (state.status == BondFormStateStatus.invalid ||
      state.status == BondFormStateStatus.pristine) {
    // Only promote to valid if all fields are now valid
    status = _allValid(false)
        ? BondFormStateStatus.valid
        : BondFormStateStatus.invalid;
  }

  state = state.copyWith(
    fields: Map.from(state.fields),
    status: status,
  );
}