DirectoryName static method

String? Function(String? value)? DirectoryName({
  1. String errorMessage = 'Invalid Directory Name',
  2. String? next(
    1. String value
    )?,
})

Returns a validator that checks if a directory name is valid.

Arguments :

  • errorMessage : The error message to return if the string does not match the pattern.
  • next : A validator to run after this validator.

Usage :

TextFormField(
validator: Validators.DirectoryName(),
),

Implementation

static String? Function(String? value)? DirectoryName({
  String errorMessage = 'Invalid Directory Name',
  String? Function(String value)? next,
}) {
  return (v) {
    if (v == null || v.trim().isEmpty) {
      return null;
    }

    // validate directory name
    if (!RegExp(r'^[a-zA-Z0-9-_]+$').hasMatch(v)) {
      return errorMessage;
    }

    if (next != null) {
      return next(v);
    }

    return null;
  };
}