getCoreUtilsAppValidatorsTemplate function
String
getCoreUtilsAppValidatorsTemplate(
- String projectName
)
Implementation
String getCoreUtilsAppValidatorsTemplate(String projectName) {
return '''
import 'package:${projectName}/core/constants/app_strings.dart';
class AppValidators {
/// Empty Field Validation
static String? requiredField(String? value, {String fieldName = "Field"}) {
if (value == null || value.trim().isEmpty) {
return "\$fieldName is required";
}
return null;
}
/// Email Validation
static String? email(String? value) {
if (value == null || value.trim().isEmpty) {
return "Email is required";
}
final regex = RegExp(r'^[\\w\\.-]+@([\\w-]+\\.)+[a-zA-Z]{2,4}\$');
if (!regex.hasMatch(value)) {
return "Enter valid email";
}
return null;
}
/// Password Validation
static String? password(String? value) {
if (value == null || value.isEmpty) {
return "Password is required";
}
if (value.length < 8) {
return "Password must be at least 8 characters";
}
final passwordRegex = RegExp(
r'^(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#\\\$&*~]).{8,}\$',
);
if (!passwordRegex.hasMatch(value)) {
return "Password must contain 1 uppercase, 1 number and 1 special character";
}
return null;
}
/// Confirm Password Validation
static String? confirmPassword(String? value, String password) {
if (value == null || value.isEmpty) {
return "Confirm password is required";
}
if (value != password) {
return "Passwords do not match";
}
return null;
}
static String? newPassword(String? value, String currentPassword) {
if (value == null || value.isEmpty) {
return "New password is required";
}
if (value.length < 8) {
return "Password must be at least 8 characters";
}
final regex = RegExp(r'^(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#\\\$&*~]).{8,}\$');
if (!regex.hasMatch(value)) {
return "Password must contain 1 uppercase, 1 number and 1 special character";
}
if (value == currentPassword) {
return "New password must be different from current password";
}
return null;
}
static String? confirmNewPassword(String? value, String newPassword) {
if (value == null || value.isEmpty) {
return "Confirm password is required";
}
if (value != newPassword) {
return "Passwords do not match";
}
return null;
}
/// Phone Number Validation
static String? phone(String? value) {
if (value == null || value.isEmpty) {
return "Phone number is required";
}
if (!RegExp(r'^[0-9]{8,15}\$').hasMatch(value)) {
return "Enter valid phone number";
}
return null;
}
/// OTP Validation
static String? otp(String? value, {int length = 4}) {
if (value == null || value.isEmpty) {
return "OTP is required";
}
if (value.length != length) {
return "OTP must be \$length digits";
}
return null;
}
/// empty value
static String? emptyValue(String? value, {String? message}) {
if (value == null || value.trim().isEmpty) {
return message ?? "value is required";
}
return null;
}
/// Name Validation
static String? name(String? value) {
if (value == null || value.trim().isEmpty) {
return "Name is required";
}
if (value.length < 2) {
return "Enter valid name";
}
return null;
}
/// last Name Validation
static String? lastName(String? value) {
if (value == null || value.trim().isEmpty) {
return AppStrings.lastNameError;
}
return null;
}
/// Number Validation
static String? number(String? value) {
if (value == null || value.isEmpty) {
return "Value is required";
}
if (!RegExp(r'^[0-9]+\$').hasMatch(value)) {
return "Only numbers allowed";
}
return null;
}
/// URL Validation
static String? url(String? value) {
if (value == null || value.isEmpty) {
return "URL is required";
}
final regex = RegExp(
r'^(https?:\\/\\/)?([\\w\\-])+\\.{1}([a-zA-Z]{2,63})([\\/\\w\\.-]*)*\\/?\$',
);
if (!regex.hasMatch(value)) {
return "Enter valid URL";
}
return null;
}
}
''';
}