validator_forge 2.2.0 copy "validator_forge: ^2.2.0" to clipboard
validator_forge: ^2.2.0 copied to clipboard

A robust and easy-to-use Flutter form validation package. Provides pre-built validators, custom regex, and a flexible builder for intuitive form handling.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:validator_forge/validator_forge.dart';

void main() {
  runApp(const MyApp());
}

/// The root widget of the example application.
class MyApp extends StatelessWidget {
  /// Creates a [MyApp] instance.
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Easy Form Validator Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(
          seedColor: const Color(0xFF6366F1), // Modern Indigo
          brightness: Brightness.light,
        ),
        useMaterial3: true,
        inputDecorationTheme: InputDecorationTheme(
          filled: true,
          fillColor: Colors.grey.shade50,
          border: OutlineInputBorder(
            borderRadius: BorderRadius.circular(12),
            borderSide: BorderSide(color: Colors.grey.shade300),
          ),
          enabledBorder: OutlineInputBorder(
            borderRadius: BorderRadius.circular(12),
            borderSide: BorderSide(color: Colors.grey.shade300),
          ),
          focusedBorder: OutlineInputBorder(
            borderRadius: BorderRadius.circular(12),
            borderSide: const BorderSide(color: Color(0xFF6366F1), width: 2),
          ),
          errorBorder: OutlineInputBorder(
            borderRadius: BorderRadius.circular(12),
            borderSide: BorderSide(color: Colors.red.shade400, width: 1.5),
          ),
        ),
      ),
      home: const DemoFormScreen(),
    );
  }
}

/// The main screen of the example application demonstrating form validation.
class DemoFormScreen extends StatefulWidget {
  /// Creates a [DemoFormScreen] instance.
  const DemoFormScreen({super.key});

  @override
  State<DemoFormScreen> createState() => _DemoFormScreenState();
}

class _DemoFormScreenState extends State<DemoFormScreen> {
  final _formKey = GlobalKey<FormState>();
  
  final _passwordController = TextEditingController();

  void _submit() {
    if (_formKey.currentState!.validate()) {
      ScaffoldMessenger.of(context).showSnackBar(
        const SnackBar(
          content: Text('Form successfully validated! 🎉'),
          behavior: SnackBarBehavior.floating,
          backgroundColor: Colors.green,
        ),
      );
    }
  }

  @override
  void dispose() {
    _passwordController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Validator Pro Demo', style: TextStyle(fontWeight: FontWeight.bold)),
        centerTitle: true,
        elevation: 0,
      ),
      body: SingleChildScrollView(
        padding: const EdgeInsets.all(24.0),
        child: Form(
          key: _formKey,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: [
              const Text(
                'Create Account',
                style: TextStyle(
                  fontSize: 28,
                  fontWeight: FontWeight.w800,
                  letterSpacing: -0.5,
                ),
              ),
              const SizedBox(height: 8),
              Text(
                'See how easy flutter_form_validator makes your life.',
                style: TextStyle(color: Colors.grey.shade600, fontSize: 16),
              ),
              const SizedBox(height: 32),
              
              // Email Field using ValidationBuilder
              TextFormField(
                decoration: const InputDecoration(
                  labelText: 'Email Address',
                  prefixIcon: Icon(Icons.email_outlined),
                ),
                keyboardType: TextInputType.emailAddress,
                validator: ValidationBuilder()
                    .required('Please enter your email')
                    .email('Please enter a valid email address')
                    .build,
              ),
              const SizedBox(height: 20),

              // Phone Field using Validators class directly
              TextFormField(
                decoration: const InputDecoration(
                  labelText: 'Phone Number',
                  prefixIcon: Icon(Icons.phone_outlined),
                ),
                keyboardType: TextInputType.phone,
                validator: (val) => Validators.phone(
                  val,
                  errorMessage: 'Must be a valid 10-digit phone number',
                ),
              ),
              const SizedBox(height: 20),

              // Age Field using Number & Minimum bounds
              TextFormField(
                decoration: const InputDecoration(
                  labelText: 'Age (numeric)',
                  prefixIcon: Icon(Icons.cake_outlined),
                ),
                keyboardType: TextInputType.number,
                validator: ValidationBuilder()
                    .required('Age is required')
                    .rule(
                      (val) => int.tryParse(val ?? '') != null && int.parse(val!) >= 18,
                      'You must be at least 18 years old',
                    )
                    .build,
              ),
              const SizedBox(height: 20),
              
              // Password Field
              TextFormField(
                controller: _passwordController,
                obscureText: true,
                decoration: const InputDecoration(
                  labelText: 'Password',
                  prefixIcon: Icon(Icons.lock_outline),
                ),
                validator: ValidationBuilder()
                    .required('Password is required')
                    .strongPassword('Mix of uppercase, lowercase, number & special char required')
                    .build,
              ),
              const SizedBox(height: 20),

              // Credit Card Field
              TextFormField(
                decoration: const InputDecoration(
                  labelText: 'Credit Card',
                  prefixIcon: Icon(Icons.credit_card_outlined),
                ),
                keyboardType: TextInputType.number,
                validator: ValidationBuilder()
                    .required('Credit Card is required')
                    .creditCard('Please enter a valid credit card format')
                    .build,
              ),
              const SizedBox(height: 20),

              // Confirm Password checking match
              TextFormField(
                obscureText: true,
                decoration: const InputDecoration(
                  labelText: 'Confirm Password',
                  prefixIcon: Icon(Icons.lock_reset_outlined),
                ),
                validator: (val) => Validators.match(
                  val,
                  _passwordController.text,
                  errorMessage: 'Passwords do not match',
                ),
              ),
              const SizedBox(height: 48),

              FilledButton(
                onPressed: _submit,
                style: FilledButton.styleFrom(
                  padding: const EdgeInsets.symmetric(vertical: 16),
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(12),
                  ),
                ),
                child: const Text('Validate Form', style: TextStyle(fontSize: 16)),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
7
likes
155
points
119
downloads

Documentation

API reference

Publisher

verified publisherhiteshpatel.tech

Weekly Downloads

A robust and easy-to-use Flutter form validation package. Provides pre-built validators, custom regex, and a flexible builder for intuitive form handling.

Repository (GitHub)
View/report issues

Topics

#flutter #form #validator #validation #regex

License

MIT (license)

Dependencies

flutter

More

Packages that depend on validator_forge