custom static method
String?
custom({
- 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,
Provides flexible custom validation with multiple configurable rules.
The most versatile validation method, allowing combination of multiple validation rules. Perfect for usernames, product codes, or custom fields.
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 Validates custom input with flexible validation rules.
Parameters:
value: The value to validatetitle: Field title for error messagestrimWhitespace: Whether to trim whitespace (default: true)isRequired: Whether empty values are allowed (default: true)minLength: Minimum allowed lengthmaxLength: Maximum allowed lengthpattern: Custom regex pattern to validate againstpatternType: Predefined pattern type (overridespatternif both provided)allowOnlyNumbers: Restrict to numeric characters only (default: false)allowOnlyLetters: Restrict to alphabetic characters only (default: false)emptyMessage: Custom empty validation messageminLengthMessage: Custom minimum length messagemaxLengthMessage: Custom maximum length messageinvalidPatternMessage: Custom invalid pattern messageinvalidNumberMessage: Custom numbers-only violation messageinvalidLettersMessage: Custom letters-only violation messagecustomValidator: Custom validation function for complex rulesexistingValues: List of existing values for uniqueness checkalreadyExistsMessage: Custom duplicate value message
Returns null if valid, or an error message if invalid.
Example:
// Validate username with custom rules
final error = SahihValidator.custom(
value: username,
minLength: 4,
maxLength: 20,
allowOnlyLetters: true,
invalidLettersMessage: 'Username must contain only letters'
);
Implementation
static String? custom({
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,
}) {
return customValidate(
value: value,
title: title,
trimWhitespace: trimWhitespace,
isRequired: isRequired,
minLength: minLength,
maxLength: maxLength,
pattern: pattern,
patternType: patternType,
allowOnlyNumbers: allowOnlyNumbers,
allowOnlyLetters: allowOnlyLetters,
emptyMessage: emptyMessage,
minLengthMessage: minLengthMessage,
maxLengthMessage: maxLengthMessage,
invalidPatternMessage: invalidPatternMessage,
invalidNumberMessage: invalidNumberMessage,
invalidLettersMessage: invalidLettersMessage,
customValidator: customValidator,
existingValues: existingValues,
alreadyExistsMessage: alreadyExistsMessage,
);
}