customValidate function
String?
customValidate({
- required String value,
- String? title,
- bool trimWhitespace = true,
- bool isRequired = true,
- int? minLength,
- int? maxLength,
- String? pattern,
- PatternType? patternType,
- bool allowOnlyNumbers = false,
- bool allowOnlyLetters = false,
- String? emptyMessage,
- String? minLengthMessage,
- String? maxLengthMessage,
- String? invalidPatternMessage,
- String? invalidNumberMessage,
- String? invalidLettersMessage,
- String? customValidator(
- String value
- List<
String> ? existingValues, - String? alreadyExistsMessage,
- SahihLanguage? lang,
Provides comprehensive custom validation with multiple configurable rules.
Flexible validation allowing combination of multiple rules. Ideal for usernames, product codes, or any field with specific requirements.
Validation order: required check, existing values, length, character restrictions, pattern matching, custom function.
Parameters:
value: The input value to validatetitle: Field name for error messages (default: "Field")trimWhitespace: Whether to trim whitespace (default: true)isRequired: Whether the field is required (default: true)minLength,maxLength: Length constraintspattern: Custom regex patternpatternType: Predefined pattern from PatternType enumallowOnlyNumbers,allowOnlyLetters: Character type restrictionscustomValidator: Custom validation functionexistingValues: List of existing values to check against- Various message parameters for customizing error messages
Implementation
String? customValidate({
required String value,
String? title,
bool trimWhitespace = true,
bool isRequired = true,
int? minLength,
int? maxLength,
String? pattern,
PatternType? patternType,
bool allowOnlyNumbers = false,
bool allowOnlyLetters = false,
String? emptyMessage,
String? minLengthMessage,
String? maxLengthMessage,
String? invalidPatternMessage,
String? invalidNumberMessage,
String? invalidLettersMessage,
String? Function(String value)? customValidator,
List<String>? existingValues,
String? alreadyExistsMessage,
SahihLanguage? lang,
}) {
final String fieldName = title ?? "Field";
final String input = trimWhitespace ? value.trim() : value;
if (isRequired && input.isEmpty) {
return emptyMessage ??
SahihLocalization.get('custom_required',
lang: lang, args: {'title': fieldName});
}
if (!isRequired && input.isEmpty) {
return null;
}
if (existingValues != null && existingValues.contains(input)) {
return alreadyExistsMessage ??
SahihLocalization.get('custom_already_exists',
lang: lang, args: {'title': fieldName});
}
if (minLength != null && input.length < minLength) {
return minLengthMessage ??
SahihLocalization.get('custom_min_length',
lang: lang,
args: {'title': fieldName, 'min': minLength.toString()});
}
if (maxLength != null && input.length > maxLength) {
return maxLengthMessage ??
SahihLocalization.get('custom_max_length',
lang: lang,
args: {'title': fieldName, 'max': maxLength.toString()});
}
if (allowOnlyNumbers && !RegExp(r'^\d+$').hasMatch(input)) {
return invalidNumberMessage ??
SahihLocalization.get('custom_invalid_number',
lang: lang, args: {'title': fieldName});
}
if (allowOnlyLetters && !RegExp(r'^[a-zA-Z]+$').hasMatch(input)) {
return invalidLettersMessage ??
SahihLocalization.get('custom_invalid_letters',
lang: lang, args: {'title': fieldName});
}
if (patternType != null && !patternType.isValid(input)) {
return invalidPatternMessage ??
SahihLocalization.get('custom_invalid_pattern',
lang: lang, args: {'title': fieldName});
}
if (pattern != null && !RegExp(pattern).hasMatch(input)) {
return invalidPatternMessage ??
SahihLocalization.get('custom_invalid_pattern',
lang: lang, args: {'title': fieldName});
}
if (customValidator != null) {
return customValidator(input);
}
return null;
}