getStringValidator method
Convenience method returning a generic validator for string input Widgets.
Implementation
StringValueValidationFunction getStringValidator(Map spec) {
var required = parseBool(spec["required"]);
final minLength = parseInt(spec["minLength"]);
final maxLength = parseInt(spec["maxLength"]);
if (minLength > 0) {
required = true;
}
final requiredText = Lowder.properties.getText(
spec["requiredMessage"] ?? "required_field_message", "errorMessage");
final minLengthText = Lowder.properties.getText(
spec["minLengthMessage"] ?? "_minimum_length_message_", "errorMessage",
attributes: {"length": minLength});
final maxLengthText = Lowder.properties.getText(
spec["maxLengthMessage"] ?? minLength == maxLength
? "_exact_length_message_"
: "_maximum_length_message_",
"errorMessage",
attributes: {"length": maxLength});
final regex = spec["regex"] != null ? RegExp(spec["regex"]) : null;
final regexMessage = Lowder.properties.getText(
spec["regexMessage"] ?? "invalid_value_message", "errorMessage");
return (value) {
var valueLength = value?.length ?? 0;
if (valueLength == 0) {
if (required) return requiredText;
return null;
}
if (valueLength < minLength) {
if (minLength == maxLength) {
return maxLengthText;
}
if (valueLength > 0) {
return minLengthText;
}
return requiredText;
}
if (maxLength > 0 && valueLength > maxLength) {
return maxLengthText;
}
if (value != null && regex != null && !regex.hasMatch(value)) {
return regexMessage;
}
return null;
};
}