From 76265977639353cf4001206390e3ede0cccaf685 Mon Sep 17 00:00:00 2001 From: Carlos Batista Date: Tue, 20 May 2025 16:59:48 -0300 Subject: [PATCH 1/3] added: CNPJ validation function to FancyValidator --- lib/fancy_validator.dart | 45 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/lib/fancy_validator.dart b/lib/fancy_validator.dart index b51faba..4e3f458 100644 --- a/lib/fancy_validator.dart +++ b/lib/fancy_validator.dart @@ -110,4 +110,49 @@ class FancyValidator { return null; }; } + + /// Returns a validation function that checks if the input value is a valid CNPJ (Brazilian company taxpayer registry). + /// + /// The validation function will return an error message if the value does not meet the CNPJ format or fails the CNPJ check, + /// otherwise it returns `null` indicating the value is valid. + /// + /// [value]: The input value to validate. + /// [errorMessage]: The error message to display if the validation fails. If not provided, a default message is used. + static String? Function() validCNPJ( + String value, { + String? errorMessage, + }) { + return () { + String cnpj = value.replaceAll(RegExp(r'\D'), ''); + + if (cnpj.length != 14 || + !RegExp(r'^\d{14}$').hasMatch(cnpj) || + RegExp(r'^(\d)\1*$').hasMatch(cnpj)) { + return errorMessage ?? 'Please enter a valid CNPJ.'; + } + + List weights1 = [5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2]; + List weights2 = [6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2]; + + int sum1 = 0; + for (int i = 0; i < 12; i++) { + sum1 += int.parse(cnpj[i]) * weights1[i]; + } + int digit1 = (sum1 * 10) % 11; + if (digit1 == 10) digit1 = 0; + + int sum2 = 0; + for (int i = 0; i < 13; i++) { + sum2 += int.parse(cnpj[i]) * weights2[i]; + } + int digit2 = (sum2 * 10) % 11; + if (digit2 == 10) digit2 = 0; + + if (digit1 != int.parse(cnpj[12]) || digit2 != int.parse(cnpj[13])) { + return errorMessage ?? 'Please enter a valid CNPJ.'; + } + + return null; + }; + } } From 7bc226b6baaa219851b5ecfc0f28e1705fc57a2e Mon Sep 17 00:00:00 2001 From: Carlos Batista Date: Tue, 20 May 2025 17:02:54 -0300 Subject: [PATCH 2/3] added: custom validation function to FancyValidator --- lib/fancy_validator.dart | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/lib/fancy_validator.dart b/lib/fancy_validator.dart index 4e3f458..db554f4 100644 --- a/lib/fancy_validator.dart +++ b/lib/fancy_validator.dart @@ -155,4 +155,20 @@ class FancyValidator { return null; }; } + + /// Returns a validation function that applies a custom validation rule. + /// + /// The validation function will apply the provided custom validation function to the input value. + /// + /// [value]: The input value to validate. + /// [validator]: The custom validation function that defines the validation logic. + /// Returns null if valid, or an error message string if invalid. + static String? Function() validField( + String value, + String? Function(String) validator, + ) { + return () { + return validator(value); + }; + } } From 983fa8fe6c26946725f480c2c9e9bbfe34ae4e2f Mon Sep 17 00:00:00 2001 From: Carlos Batista <51524347+carlosbatistadev@users.noreply.github.com> Date: Tue, 24 Jun 2025 11:24:24 -0300 Subject: [PATCH 3/3] Update pubspec.yaml --- pubspec.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pubspec.yaml b/pubspec.yaml index 8a1ed69..1358981 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: fancy_form description: A flexible and easy-to-use form management solution for Flutter. -version: 1.2.0 +version: 1.3.0 repository: https://github.com/MusilyApp/fancy_form environment: @@ -22,4 +22,4 @@ dev_dependencies: flutter: uses-material-design: true - \ No newline at end of file +