validate method

  1. @override
String? validate({
  1. required String label,
  2. required String? value,
})
override

Validates that the given value is in a valid currency format. If allowNegative is true then this will pass on negative currency values, other this will fail on negative currency values.

This will pass on empty or null values.

See also:

Implementation

@override
String? validate({
  required String label,
  required String? value,
}) {
  String? error;

  if (value?.isNotEmpty == true) {
    num? d;
    try {
      d = NumberFormat.currency().parse(value ?? '');
    } catch (e) {
      // no-op, it simply failed parsing
    }

    if (d == null) {
      error = translate(
        FormValidationTranslations.form_validation_currency,
        {
          'label': label,
        },
      );
    } else if (d < 0 && allowNegative != true) {
      error = translate(
        FormValidationTranslations.form_validation_currency_positive,
        {
          'label': label,
        },
      );
    }
  }

  return error;
}