formCellValidator function Null safety

String? formCellValidator(
  1. dynamic value,
  2. bool required,
  3. String? requiredMessage,
  4. EComponentType componentType,
  5. QUFormComponentOptionsModel? componentOptions
)

Implementation

String? formCellValidator(value, bool required, String? requiredMessage,
    EComponentType componentType, QUFormComponentOptionsModel? componentOptions) {
  String? message;
  final valueIsNull = (value == '' ||
          value == [] ||
          value == null ||
          (value is List && value.isEmpty));
  if (required == true && valueIsNull) {
    message = requiredMessage;
    return message;
  }
  if(required == false && valueIsNull) {
    return null;
  }
  switch (componentType) {
    case EComponentType.INPUT_NUMBER:
      final isNum = getValidator(EQUFormValidatorType.NUMBER, value.toString());
      if (!isNum) {
        message = '请输入正确的数字';
        break;
      }
      final newValue = value is num
          ? value
          : value is String
              ? num.tryParse(value)
              : null;
      if (newValue == null) {
        message = '请输入正确的数字';
        break;
      }
      final min = componentOptions?.min ?? -9007199254740991;
      final max = componentOptions?.max ?? 9007199254740991;
      if (newValue < min || newValue > max) {
        message = '请输入小于$max, 大于$min的数字';
      } else {
        message = null;
      }
      break;
    default:
  }
  return message;
}