validation_extensions 1.0.1
validation_extensions: ^1.0.1 copied to clipboard

Flutter form field validations using dart extension methods.

validation_extensions #

Flutter form field validations using dart extension methods.

Available validation extension methods #

String extension methods #

Check tests file for examples on how to use them or the example app.

MethodParametersDescription
isRequiredNoneValidates if the string in not empty
isEmailNoneValidates if the string is a valid email address
minLengthint minValidates if the string length is longer than min
maxLengthint maxValidates if the string length is shorter than max
lengthRangeint min, int maxValidates if the string length is longer than min and shorter than max
matchString stringToMatchValidates if the string is an exact match of the String stringToMatch parameter that you provide
matchPatternString regExpValidates if the string matches the regular expression regExp parameter that you provide
isIntNoneValidates if the string is a valid integer
isDoubleNoneValidates if the string is a valid double
minint minValidates if the string is a valid double bigger than min
maxint maxValidates if the string is a valid double smaller than max
rangeint min, int maxValidates if the string is a valid double between min and max

Note

You have to use isRequired to make a field mandatory, all other extension methods won't validate empty string, and will validate only if string is not empty which will make the field optional.

List<Function> extension methods #

MethodDescription
validateexecute validation functions one by one, will stop execution when it encounter a validation error and return the errorText

Usage #

All extension methods return type is Function, you have to use double parentheses to execute the returned function for single validations.

Form(
      key: _formKey,
      child: TextFormField(
          validator: (v) => v.isRequired()(),
        ),
      ),

Multi validations #

For multi rules validation put your validation in a list and call validate() extension method on the list, it will execute validation one by one, it will stop execution when it encounter the first validation error and return the errorText to increase performance.

Form(
      key: _formKey,
      child: Column(
        children: <Widget>[
          TextFormField(
            onChanged: (v) => password = v,
            validator: (v) => [
              v.isRequired(),
              v.minLength(8),
            ].validate(),
          ),
          TextFormField(
            validator: (v) => [
              v.isRequired(),
              v.match(password),
            ].validate(),
          ),
        ],
      ),
    ),

Custom validations #

If you want to use you own validations in multi rule validation, your validation rules must be: Function that returns a Function that returns a String.

Function customValidation(v) {
	return(){
			return v != "Validation logic" ? "Invalid input" : null;
	};
}

Form(
     key: _formKey,
     child: TextFormField(
        autovalidate: true,
        validator: (v) => [
			v.isRequired(),
	        customValidation(v),
        ].validate(),
      ),
    ),

Custom error message #

Pass the optional named parameter errorText to the extension method to override the default error message

Form(
      key: _formKey,
      child: TextFormField(
        onChanged: (v) => password = v,
        validator: (v) => [
          v.isRequired(
            errorText: "Password is required",
          ),
          v.minLength(
            8,
            errorText: "Password can not be less than 8 characters",
          ),
        ].validate(),
      ),
    ),

15
likes
90
pub points
63%
popularity

Flutter form field validations using dart extension methods.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

Dependencies

flutter

More

Packages that depend on validation_extensions