Skip to content

Latest commit

 

History

History
99 lines (81 loc) · 2.49 KB

README.md

File metadata and controls

99 lines (81 loc) · 2.49 KB

A simple package to validate inputs in flutter.

Sample PValidator

Features

Avaliable rules

  • Accept values: list of accepted values
  • Date: test with DateTime.tryParse
  • Email: email validate
  • Max numeric: check max number
  • Max string: check max string size
  • Min numeric: check min number
  • Min string: check min string size
  • Numeric: check value is numeric
  • Regexp: validate with regular expression
  • Required: check if exist value
  • RequiredDate: check if exist value
  • Requiredif: check if exist value if
  • Same: value must be same
  • Size string: value size check
  • Minimum date time: parse string and check is date is after the minimum
  • maximum date time: parse string and check is date is before the maximum
  • Size string: value size check
  • Integer: check value is a integer
  • Only one: check if only one value was entered

packages.yaml

pvalidator: <lastest version>

Usage

Mandatory field validation example:

TextFormField(
  validator: (string) {
    return PValidator([
      PRuleRequired(string),
    ]).val();
  },
)

PValidator is the master class. PRuleRequired is the validate rule. Set one or more rules to validate.

Validate with multiple rules:

TextFormField(
  validator: (string) {
    return PValidator([
      PRuleRequired(string),
      PRuleSizeString(string, 5),
    ]).val();
  },
)

Create your custom rule

import 'package:pvalidator/src/rules/rule.dart';

class MyCustomRule implements Rule {
  String? _value;
  String message;

  MyCustomRule(this._value, {this.message = "Value must be equal test"});

  @override
  String? validate() {
    if (_value == null || _value?.trim().length == 0) {
      return null;
    } else if (_value != "test") {
      return message;
    } else {
      return null;
    }
  }
}