isColorField static method

ValidatorEvent isColorField()

Validator to check if a field contains a valid color code. The color code must be a valid hexadecimal color code. The validator checks whether the provided value is non-null, is a non-empty string, and matches the format of a color code. If the validation fails, an error message 'error.field.color' is returned.

Implementation

static ValidatorEvent isColorField() => (value) async {
      var colorRegexp = RegExp(r'^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$');
      var res = value != null &&
          value.toString().trim().isNotEmpty &&
          colorRegexp.hasMatch(value.toString());

      return FieldValidateResult(
        success: res,
        error: res ? '' : 'error.field.color',
      );
    };