requiredFieldMultiLanguage static method

ValidatorEvent requiredFieldMultiLanguage()

Validator to check if a field is required in multiple languages.

This validator expects a JSON object with language keys and checks if at least one value is non-null and non-empty.

Implementation

static ValidatorEvent requiredFieldMultiLanguage() {
  return (value) {
    var res = (value != null && value.toString().trim().isNotEmpty);

    if (!res) {
      return FieldValidateResult(
        success: res,
        error: res ? '' : 'error.field.required',
      );
    }

    Map<String, String> resMap = {};

    try {
      var json = jsonDecode(value.toString());
      for (var key in json.keys) {
        if (json[key] != null && json[key]!.trim().isNotEmpty) {
          resMap[key] = json[key]!.trim();
        }
      }
    } catch (e) {
      resMap = {};
    }

    return FieldValidateResult(
      success: resMap.isNotEmpty,
      error: resMap.isNotEmpty ? '' : 'error.field.required',
    );
  };
}