compareFields static method

String? compareFields(
  1. dynamic value,
  2. dynamic otherFieldValue,
  3. String operator,
  4. String fieldName,
  5. String otherFieldName,
)

Cross-field validation - compare two fields with a comparison operator.

value - Current field value otherFieldValue - Value to compare against operator - Comparison operator ('>', '<', '>=', '<=', '==', '!=') fieldName - Name of current field otherFieldName - Name of other field

Implementation

static String? compareFields(
  dynamic value,
  dynamic otherFieldValue,
  String operator,
  String fieldName,
  String otherFieldName,
) {
  if (value == null) return null;

  // Try to parse as numbers for numeric comparison
  final numValue = num.tryParse(value.toString());
  final numOther = num.tryParse(otherFieldValue?.toString() ?? '');

  if (numValue != null && numOther != null) {
    switch (operator) {
      case '>':
        if (!(numValue > numOther)) {
          return '$fieldName must be greater than $otherFieldName.';
        }
        break;
      case '<':
        if (!(numValue < numOther)) {
          return '$fieldName must be less than $otherFieldName.';
        }
        break;
      case '>=':
        if (!(numValue >= numOther)) {
          return '$fieldName must be greater than or equal to $otherFieldName.';
        }
        break;
      case '<=':
        if (!(numValue <= numOther)) {
          return '$fieldName must be less than or equal to $otherFieldName.';
        }
        break;
      case '==':
        if (numValue != numOther) {
          return '$fieldName must equal $otherFieldName.';
        }
        break;
      case '!=':
        if (numValue == numOther) {
          return '$fieldName must not equal $otherFieldName.';
        }
        break;
    }
  }

  return null;
}