themisir / form-validator Goto Github PK
View Code? Open in Web Editor NEWSimplest form validation for flutter form widgets
Home Page: https://pub.dev/packages/form_validator
License: MIT License
Simplest form validation for flutter form widgets
Home Page: https://pub.dev/packages/form_validator
License: MIT License
Is your feature request related to a problem? Please describe.
I dont know if is possible but i tried to Passing the data in the text editing controller to the validation builder (my custom validation builder extension) but it doesnt work.
Describe the solution you'd like
I want to send the string data I got from the password controller to custom Validator Builder. When I tested it I noticed that, string data in the text editing controller does not reach the validation builder. But then when I try to access it with the regular button, the text editing controller works fine.
Additional context
Text Form Field codes:
child: TextFormField(
controller: _repeatPassController,
validator: ValidationBuilder(
requiredMessage: "Bu alan gereklidir!")
.repeatPassword(_passController.text)
.build(),
obscureText: true,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Tekrar Şifre',
hintText: 'Güvenli bir şifre giriniz'),
),
This part of my codes is kept in another file called consts.dart.
extension CustomValidationBuilderRepeatPassword on ValidationBuilder {
///check if password repeated
///if password is not repeated return error message
repeatPassword(String password) => add((value) {
print("password: $password");
if (value == null || value.isEmpty) {
return "Bu alan gereklidir";
}
if (value != password) {
print(value + " value");
print(password + " password");
return 'Şifreler eşleşmiyor!';
}
return null;
});
}
Describe the bug
A clear and concise description of what the bug is.
To Reproduce
Steps to reproduce the behavior:
Expected behavior
A clear and concise description of what you expected to happen.
Screenshots
If applicable, add screenshots to help explain your problem.
Flutter doctor
Open terminal, run "flutter doctor" and copy output here.
Additional context
Add any other context about the problem here.
Passing ValidationBuilder()
to validator has this error:
This is my code:
TextField(
controller: passwordController,
labelText: 'Password',
hintText: 'Password',
obscureText: true,
validator: ValidationBuilder().required().minLength(8).build(),
),
And I'm my custom TextField()
is
class TextField extends StatelessWidget {
final String labelText;
final String hintText;
final bool? obscureText;
final TextEditingController controller;
final FormFieldValidator? validator;
const TextField({
Key? key,
required this.labelText,
required this.hintText,
this.obscureText,
required this.controller,
this.validator,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return TextFormField(
controller: controller,
textInputAction: TextInputAction.next,
decoration: InputDecoration(
border: const OutlineInputBorder(),
hintText: hintText,
labelText: labelText,
),
obscureText: obscureText ?? false,
validator: validator,
);
}
}
As far as i'm aware, there is no way to add both a new custom validation rule and localized error message associated.
It would be great if we could extend both ValidationBuilder
and FormValidatorLocale
derived classes (eg: LocaleEn
) or rethink a bit the way to handle it to enable this feature.
For now the solution is to copy all the localized files and handle the loading according to user's language, which is not very practical.
Describe the bug
I am using global validator builder, because i want to have the same locale everywhere. bue i have noticed that whenever i am using the optional: true
parameter, it overrides future required()
method. i feel like it should not be an intended behaviour since the constructor of ValidationBuilder
is using required()
if (!optional) required(requiredMessage);
it should base only on that or change flag optional
when we are using required()
I noticed that the package missed a number input validator which make it possible to check if the input contains only digits.
Can we add this feature
Is it possible to get bool also from validation result? I need to validate form in logic also not only in UI so if there is something like ValidationBuilder().email().isValid('[email protected]')
is will be nice. Can this be achieved using extension?
I have a textformfield in which i want user can leave that i.e not required field but if it fill should be less than some double value. I have keyboard formattor to set to digits only.
It seems the field is always required
final ValidationBuilder validationBuilder = ValidationBuilder().email('Wrong Email Format');
String? _errorText = validationBuilder.build()(null);
Result "The field is required"
final ValidationBuilder validationBuilder = ValidationBuilder().email('Wrong Email Format');
String? _errorText = validationBuilder.build()('');
Result "Wrong Email Format"
Is your feature request related to a problem? Please describe.
If I want to add a custom locale and switch to another local on runtime,
When the user change locale, for custom local I need to use the globalLocale, while for supported locale I need to use setLoacel()
Describe the solution you'd like
I want a method like:
ValidationBuilder.addLocale(FormValidatorLocale customLocale);
This issue contains tasks required to be accomplished before v1.0 release.
I'm planning to release a major update for form_validator to "fix" or "improve" some issues on existing releases.
Here's some of goals I'm planning to improve on next version:
required
and optional
validation rules - related issues: #27 #29 #30user@domain
while that's accepted by specs, in real world use cases it's preferred to validate emails to have 2nd level domains. Preferably we can also make email validator customizable so users can use custom regexs for their use case - related issues: #26If you're currently using form_validator
and have any suggestions please let me know below, I'm currently no using this package myself so I'm not sure how else this could be improved further.
Also thanks for being involved in extending localization support and providing various feedbacks 😇
Is your feature request related to a problem? Please describe.
Make available to use ValidationBuilder not only for TextFormField, but for other elements like DropdownButtonFormField
Describe the solution you'd like
DropdownButtonFormField(
validator: ValidationBuilder().required().build(),
),
Right now it's returning error
"The argument type 'String? Function(String?)' can't be assigned to the parameter type 'String? Function(dynamic)?'. (Documentation)"
it seems there is copy-paste typo in url
method documentation:
https://github.com/TheMisir/form-validator/blob/68f55fdedab2a4b49d7555057bb75cd277fdcdca/lib/src/validator_builder.dart#L150
Any plan to support/migrate to null-safety?
I need help to add new languages to this library. You can simply duplicate any file in /lib/src/i18n
(for example en.dart
) and translate messages. Then please create pull request.
Thanks.
Is your feature request related to a problem? Please describe.
How to add a prefix to the message? eg. Field-A is required, Field-B is required, etc.
Describe the solution you'd like
Perhaps, all of the rule can have field as first parameter. So that field can be add to the message.
ValidationBuilder required() => add((String? field, value) {
return (value ?? "").isEmpty ? '${(field ?? "Field")} is required' : null;
});
ValidationBuilder(prefix: "Field-A").required()
ValidationBuilder(prefix: "Field-B").required()
Is your feature request related to a problem? Please describe.
When using the required validator on a FormField
, that can have non String
-type values, like many custom FormFields
do (like a DatePicker
), the current required validator throws an exception, since it tests on value.length > 0
. Some FormField
s may even return null.
One can imagine FormField
s that allow an empty array as valid value, but not null. In this case the current required
validator will not work as expected.
Describe the solution you'd like
Replacing required
validator with adding notNull
and notBlank
. Or keeping required
but stating it does only work on type String
.
Describe alternatives you've considered
none
Additional context
See my pull request
Describe the bug
PatternValidator(RegExp(r".*"))
is a legal based on the method signature, but it doesn't work as expected.
PatternValidator(RegExp(r".*")).isMatch("text")
returns false
.
To Reproduce
PatternValidator(RegExp(r".*"))
is expected to match any text, but it isn't
Expected behavior
PatternValidator(RegExp(r".*")).isMatch("text")
should return true
, but it returns false
.
Flutter doctor
[✓] Flutter (Channel beta, 2.1.0-12.2.pre, on Mac OS X 10.15.5 19F101 darwin-x64, locale en-AU)
• Flutter version 2.1.0-12.2.pre at /Users/tim.wen/Workspace/flutter
• Framework revision 5bedb7b1d5 (3 weeks ago), 2021-03-17 17:06:30 -0700
• Engine revision 711ab3fda0
• Dart version 2.13.0 (build 2.13.0-116.0.dev)
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
• Android SDK at /usr/local/lib/Android/sdk
• Platform android-30, build-tools 30.0.3
• ANDROID_HOME = /usr/local/lib/Android/sdk
• Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6915495)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS
• Xcode at /Applications/Xcode.app/Contents/Developer
• Xcode 12.4, Build version 12D4e
• CocoaPods version 1.10.1
[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
[✓] Android Studio
• Android Studio at /Applications/Android Studio 4.2 Preview.app/Contents
• Flutter plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)
[✓] Android Studio (version 4.1)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6915495)
[✓] VS Code (version 1.55.0)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.21.0
[✓] Connected device (3 available)
• iPhone 12 Pro (mobile) • 8E043A16-17CB-44E0-ADEF-9AD872895BAA • ios • com.apple.CoreSimulator.SimRuntime.iOS-14-4 (simulator)
• macOS (desktop) • macos • darwin-x64 • Mac OS X 10.15.5 19F101 darwin-x64
• Chrome (web) • chrome • web-javascript • Google Chrome 89.0.4389.114
• No issues found!
Additional context
The fundamental issue isValid
method, which convert the pattern to String
by calling toString
.
@override
bool isValid(String? value) =>
hasMatch(pattern.toString(), value!, caseSensitive: caseSensitive);
Pattern
has 2 implementations: String.toString
returns itself. but RegExp.toString
doesn't return its pattern.
RegExp(r".*").toString()
returns RegExp/.*/
.
Validation of common idioms via regexp is a tricky business and not something you really want to do yourself. For example, the regexp currently being used to validate emails is actually quite simple:
If we were to compare it to the one being define in the validators package:
Obviously the latter take into account Unicode characters and probably some other quirks.
I believe it would be better to simply leverage the validators package instead of trying to come up with regexps on our own that we have to maintain and test.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.