0 purchases
awesome validator
awesome_validator #
A flutter form field validator that provides common validation options.
Usage #
//Basic Validator
TextFormField(
validator: EmailValidator(errorText: 'Enter a valid email address')
);
copied to clipboard
Multi validator #
final passwordValidator = MultiValidator([
RequiredValidator(errorText: 'Password is required'),
MinLengthValidator(8, errorText: 'Password must be at least 8 digits long'),
]);
final TextEditingController password = TextEditingController();
Form(
key: formKey,
child: Column(children: [
TextFormField(
obscureText: true,
onChanged: (val) => password = val,
validator: passwordValidator,
),
]),
);
copied to clipboard
Example #
import 'package:awesome_validator/awesome_validator.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(title: 'Awesome Validator'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final GlobalKey<FormState> formKey = GlobalKey<FormState>();
final TextEditingController password = TextEditingController();
final TextEditingController email = TextEditingController();
final passwordValidator = MultiValidator([
RequiredValidator(errorText: 'password is required'),
MinLengthValidator(8, errorText: 'password must be at least 8 digits long'),
]);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Form(
key: formKey,
child: Column(
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
controller: email,
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: 'Enter Email',
),
validator: EmailValidator(
errorText: 'Please enter valid email'),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
controller: password,
obscureText: true,
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: 'Enter Password',
),
validator: passwordValidator,
),
),
],
)),
),
ElevatedButton(
onPressed: () {
if (formKey.currentState!.validate()) {
// Form is validated, do something
print('Form is validated!');
}
},
child: const Text('Submit'),
),
],
),
),
);
}
}
copied to clipboard
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.