ez_validator 0.2.2 copy "ez_validator: ^0.2.2" to clipboard
ez_validator: ^0.2.2 copied to clipboard

Dead simple field/object schema validation for flutter,EzValidator api is inspired by Yup

example/lib/main.dart

import 'package:ez_validator/main.dart';
import 'package:ez_validator_example/error_widget.dart';
import 'package:ez_validator_example/fr.dart';
// import 'package:ez_validator_example/french_locale.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';

void main() {
  /// set this in the main to set your custom locale
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'exemple',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'EzValidator Exemple'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, this.title}) : super(key: key);

  final String? title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  Map<String, dynamic> form = {
    "number": '5',
    "pos": '-5',
    "date": "00-00-00", //DateTime.now().toIso8601String()
  };
  Map<String?, String?> errors = {};

  EzSchema mySchema = EzSchema.shape(
    {
      "email": EzValidator(label: "l'email").required().email().build(),
      "password": EzValidator(label: 'Mot de pass')
          .required()
          .minLength(6)
          .matches(
              r'^(?=.*[A-Za-z])(?=.*\d)(?=.*[@$!%*#?&])[A-Za-z\d@$!%*#?&]{6,}$',
              'at least one letter, one number and one special character')
          .build(),
      "age": EzValidator(label: 'Age', defaultValue: 22)
          .required()
          .min(18)
          .build(),
      "number": EzValidator().required().positive().build(),
      "pos": EzValidator().required().negative().build(),
      "date": EzValidator(label: 'Date').required().date().build(),
      "birth_year": EzValidator(label: 'Date of Birth')
          .required()
          .min(2012)
          .max(2021)
          .build(),
    },
  );

  validate() {
    /// wrap your validation method with try..catch when you add the
    /// identicalKeys clause to your schema
    try {
      setState(() {
        errors = mySchema.validateSync(form);
      });
      // ignore: avoid_print
      errors.forEach((key, value) {
        // ignore: avoid_print
        print('$key ===> $value');
      });
    } catch (e) {
      Fluttertoast.showToast(
        msg: "Missing fields input",
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.BOTTOM,
        timeInSecForIosWeb: 1,
        backgroundColor: Colors.red,
        textColor: Colors.white,
        fontSize: 16.0,
      );
    }
  }

  InputDecoration _getInputDecoration(IconData icon, String label) {
    return InputDecoration(
      prefixIcon: Icon(icon),
      border: InputBorder.none,
      fillColor: const Color(0xfff3f3f4),
      filled: true,
      hintText: label,
    );
  }

  _onChange(String name, String value) {
    form[name] = value;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title as String),
      ),
      body: Center(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Expanded(
              child: Container(
                color: Colors.black12,
                child: Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 25.0),
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      TextField(
                        onChanged: (value) => _onChange('email', value),
                        decoration: _getInputDecoration(Icons.email, "Email"),
                      ),
                      ErrorWIdget(name: errors['email']),
                      const SizedBox(height: 10.0),
                      TextField(
                        onChanged: (value) => _onChange('password', value),
                        decoration:
                            _getInputDecoration(Icons.password, "Password"),
                      ),
                      ErrorWIdget(name: errors['password']),
                      const SizedBox(height: 10.0),
                      TextField(
                        keyboardType: TextInputType.number,
                        onChanged: (value) => _onChange('age', value),
                        decoration: _getInputDecoration(
                            Icons.supervised_user_circle_outlined, "Age"),
                      ),
                      ErrorWIdget(name: errors['age']),
                      const SizedBox(height: 10.0),
                      TextField(
                        keyboardType: TextInputType.number,
                        onChanged: (value) => _onChange('birth_year', value),
                        decoration: _getInputDecoration(
                            Icons.date_range_outlined, "Birth Year"),
                      ),
                      ErrorWIdget(name: errors['birth_year']),
                      const SizedBox(height: 10.0),
                      MaterialButton(
                        onPressed: validate,
                        color: Colors.white,
                        highlightColor: Colors.red,
                        child: const Text("Submit"),
                      ),
                      MaterialButton(
                        onPressed: () {
                          EzValidator.setLocale(const FrLocale());
                        },
                        color: Colors.white,
                        highlightColor: Colors.red,
                        child: const Text("Fr Locale"),
                      )
                    ],
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
40
likes
0
pub points
82%
popularity

Publisher

verified publisheriheb.tech

Dead simple field/object schema validation for flutter,EzValidator api is inspired by Yup

Repository (GitHub)
View/report issues

Documentation

Documentation

License

unknown (license)

More

Packages that depend on ez_validator