All Projects → themisir → form-validator

themisir / form-validator

Licence: MIT license
Simplest form validation for flutter form widgets

Programming Languages

dart
5743 projects
HTML
75241 projects
swift
15916 projects

Projects that are alternatives of or similar to form-validator

Form bloc
🔥 Dart and Flutter Package 🔥 Easy Form State Management using BLoC pattern 🔥 Wizard/stepper forms, asynchronous validation, dynamic and conditional fields, submission progress, serialization and more! 🔥
Stars: ✭ 239 (+408.51%)
Mutual labels:  form-validation
recaptcha2
Easy verifier for google reCAPTCHA version 2 for Node.js and Express.js
Stars: ✭ 48 (+2.13%)
Mutual labels:  form-validation
tform
A easy, extensible and dynamic flutter form framework. Support for custom selectors, validators and widgets. Support form verification, insert, delete and so on.
Stars: ✭ 30 (-36.17%)
Mutual labels:  flutter-form-widgets
formz
A unified form representation in Dart used at Very Good Ventures 🦄
Stars: ✭ 262 (+457.45%)
Mutual labels:  form-validation
react-inputs-validation
A react component for form inputs validation. Online demo examples
Stars: ✭ 48 (+2.13%)
Mutual labels:  form-validation
vue-formly-buefy
The declarative way to create and validate forms.
Stars: ✭ 18 (-61.7%)
Mutual labels:  form-validation
Angular Validation Match
Checks if one input matches another. Useful for confirming passwords, emails, or anything.
Stars: ✭ 228 (+385.11%)
Mutual labels:  form-validation
react-useForm
World's simplest React hook to manage form state
Stars: ✭ 30 (-36.17%)
Mutual labels:  form-validation
ATGValidator
iOS validation framework with form validation support
Stars: ✭ 51 (+8.51%)
Mutual labels:  form-validation
react-form-validation
A simple and declarative way to validate your forms in React
Stars: ✭ 24 (-48.94%)
Mutual labels:  form-validation
laminas-form
Validate and display simple and complex forms, casting forms to business objects and vice versa
Stars: ✭ 65 (+38.3%)
Mutual labels:  form-validation
Frontend-Learning-Journey
Tutorials, definitions, frameworks and some of the projects i made when starting to learn frontend web developement
Stars: ✭ 28 (-40.43%)
Mutual labels:  form-validation
FilterInputJs
Tiny and Powerful Library for limit an entry (text box,input) as number,string or more...
Stars: ✭ 37 (-21.28%)
Mutual labels:  form-validation
Final Form
🏁 Framework agnostic, high performance, subscription-based form state management
Stars: ✭ 2,787 (+5829.79%)
Mutual labels:  form-validation
AndroidVerify
Android library designed for rapid and customizable form validation.
Stars: ✭ 41 (-12.77%)
Mutual labels:  form-validation
Svelte Forms Lib
📝. A lightweight library for managing forms in Svelte
Stars: ✭ 238 (+406.38%)
Mutual labels:  form-validation
envalid
Envalid is a framework agnostic and fluent server side form validation package for PHP
Stars: ✭ 23 (-51.06%)
Mutual labels:  form-validation
django-front-end-validators
Use model field validator functions for front end JS form validation
Stars: ✭ 15 (-68.09%)
Mutual labels:  form-validation
antd-react-form-builder
Example
Stars: ✭ 74 (+57.45%)
Mutual labels:  form-validation
ember-validity-modifier
Ember Octane addon to add custom validity (form validation) to form fields
Stars: ✭ 28 (-40.43%)
Mutual labels:  form-validation

form_validator

pub package GitHub GitHub Workflow Status codecov

Simplest form validation for flutter form widgets.

  • Zero dependency
  • Localization supported
  • Extensible
  • Well tested
  • Open source

Example

TextFormField(
  validator: ValidationBuilder().email().maxLength(50).build(),
  decoration: InputDecoration(labelText: 'Email'),
),

live demo

Getting Started

Installation

Add form_validator as dependency to your flutter project by adding this lines to pubspec.yaml.

dependencies:
  form_validator: ">=0.1.1 <2.0.0"

Then run flutter pub get to install required dependencies.

Check installation tab for more information

Code

Import form_validator package to your dart widgets by writing:

import 'package:form_validator/form_validator.dart';

Now you can use ValidationBuilder class to create validation logic. Here's simple validator that validates if input length is between 10 and 50 characters long.

final validate = ValidationBuilder().minLength(10).maxLength(50).build();

print(validate('test'));        // Minimum length must be at least 10 characters
print(validate('Hello World')); // null, means value is correct

Localization

You can set global locale using ValidationBuilder.setLocale(String localeName) or you can also define locale on ValidationBuilder({ String localeName }) constructor.

ValidationBuilder.setLocale('en');

// will use *en* localization
ValidationBuilder()
  .minLength(5)
  .build();

// will use *az* localization
ValidationBuilder(localeName: 'az')
  .minLength(5)
  .build();

// will display custom message
ValidationBuilder()
  .minLength(5, 'Length < 5 😟')
  .build();

Methods

.minLength(int minLength, [String message])

Validates if value length is not less than minLength.

.maxLength(int maxLength, [String message])

Validates if value length is not more than maxLength.

.email([String message])

Checks if value is an email address.

.phone([String message])

Checks if value is an phone number.

Note: This method might be changed in future.

.ip([String message])

Checks if value is correct IPv4 address.

.ipv6([String message])

Checks if value is correct IPv6 address.

.url([String message])

Checks if value is correct url address.

.regExp(RegExp regExp, String message)

Validates if value does matches regExp or not.

message argument is required in this method.

.or(Action<ValidationBuilder> left, Action<ValidationBuilder> right, {bool reverse = false})

Validates if value passes any of left and right validations. If value doesn't passes any error message from right validation will be displayed (If reverse set to true message from left validation will be displayed).

Example:

print(
  ValidationBuilder().or(
    (builder) => builder.email('not email'),
    (builder) => builder.phone('not phone'),
  )
  .test('invalid')
); // not phone

print(
  ValidationBuilder().or(
    (builder) => builder.email('not email'),
    (builder) => builder.phone('not phone'),
    reverse: true,
  )
  .test('invalid')
); // not email

Notes

ValidationBuilder is mutable.

You need to construct different instances for each validation.

Wrong

final builder = ValidationBuilder().email();

TextFormField(
  validator: builder.maxLength(50).build(),
),
TextFormField(
  validator: builder.maxLength(20).build(),
),

Correct

final validator1 = ValidationBuilder().email().maxLength(50).build();
final validator2 = ValidationBuilder().email().maxLength(20).build();

TextFormField(
  validator: validator1,
),
TextFormField(
  validator: validator2,
),

Add custom localization

Firstly you need to extend abstract FormValidatorLocale class.

import 'package:form_validator/form_validator.dart';

class MyValidationLocale extends FormValidatorLocale {
  @override
  String name() => "custom";

  @override
  String required() => "Field is required";

  ...
}

PROTIP: You can copy a language file from /lib/src/i18n folder and modify messages as you want.

Then you can use your custom locale class as global or local validation locale.

// global locale
void main() {
  ValidationBuilder.globalLocale = MyValidationLocale();
  ...
}

// local locale
final locale = MyValidationLocale();

build(BuildContext context) {
  final emailValidator = ValidationBuilder(locale: locale)
    .email()
    .build();
  ...
}

PROTIP: Feel free to add your locale to library by opening pull request TheMisir/form-validator to support library.

Extending ValidationBuilder

You can use dart extension methods to extend ValidationBuilder.

extension CustomValidationBuilder on ValidationBuilder {
  password() => add((value) {
    if (value == 'password') {
      return 'Password should not "password"';
    }
    return null;
  });
}

final validator = ValidationBuilder().password().build();

Support

If you would like to support this library you could translate messages to your own language.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].