checkByRegexp static method
Validator to check if a field matches a given regular expression pattern.
The validator checks whether the provided value matches the specified regex pattern.
If the value does not match the pattern, an error message 'error.field.regex'
is returned.
Parameters:
pattern
: The regular expression pattern to match against. (required)isRequired
: Whether the field is required (non-null). Defaults totrue
. Returns:ValidatorEvent
: A validator event function that can be used in theFormValidator
.
Implementation
static ValidatorEvent checkByRegexp(
RegExp pattern, {
bool isRequired = true,
}) {
return (value) async {
if ((value == null || value.toString().isEmpty) && isRequired) {
return FieldValidateResult(
success: false,
error: 'error.field.required',
);
}
if ((value == null || value.toString().isEmpty) && !isRequired) {
return FieldValidateResult(success: true);
}
if (!pattern.hasMatch(value.toString())) {
return FieldValidateResult(
success: false,
error: 'error.field.regex',
);
}
return FieldValidateResult(success: true);
};
}