milad-akarie / form_field_validator Goto Github PK
View Code? Open in Web Editor NEWLicense: MIT License
License: MIT License
FormBuilderValidators.numeric should accept double value like 5.5
hello,
i've notice the FormBuilderValidators.numeric is not accepting float or double value it's working same as integer
not accepting the dot
thanks in advance for help
The Validation Error message Automatically vanishes when scroll down or after scrolling it out the viewport, in the bunch of list of form text input field.
I hunted for 2 hours to understand why my field error messages are going nuts. And this is why.
I use nested MultiValidator
instances and they are malfunctioning due to this static field. Please fix this. I can't do it without changing several base classes because you have made the errorText
field final!
Here's an improved version of TypedMultiValidator
I use, which also has type support.
import 'package:form_field_validator/form_field_validator.dart';
class TypedMultiValidator<T> extends FieldValidator<T> {
final List<FieldValidator<T>> validators;
// error test is no longer static
String _errorText = '';
// the empty string passed to super is not used
TypedMultiValidator(this.validators) : super('');
@override
bool isValid(value) {
for (FieldValidator<T> validator in validators) {
// ignores null validators
if (validator == null) continue;
// uses the called output instead of using raw error message
final validationResult = validator.call(value);
if (validationResult != null) {
_errorText = validationResult;
return false;
}
}
return true;
}
@override
String call(dynamic value) {
return isValid(value) ? null : _errorText;
}
}
Looks like isValid methods are missing checks for null values.
For example, in RequiredValidator:
@override
bool isValid(String? value) {
return value!.isNotEmpty;
}
The issue leads to incompatibility with widgets like DropdownButtonFormField which may provide null values for validation and this code causes an exception.
It can be easily fixed by adding a null check for the method above:
@override
bool isValid(String? value) {
return value != null && value.isNotEmpty;
}
Hi, I am using the RequiredValidator on a DropdownButtonFormField widget. When the validation returns false (no value is selected), an exception is thrown:
The following JSNoSuchMethodError was thrown while handling a gesture:
NoSuchMethodError: invalid member on null: 'isNotEmpty'
When the exception was thrown, this was the stack:
packages/form_field_validator/form_field_validator.dart 50:17 isValid
packages/form_field_validator/form_field_validator.dart 55:12 call
packages/flutter/src/widgets/form.dart 392:27 [_validate]
packages/flutter/src/widgets/form.dart 385:7 <fn>
packages/flutter/src/widgets/framework.dart 1233:30 setState
packages/flutter/src/widgets/form.dart 384:5 validate
packages/flutter/src/widgets/form.dart 209:24 [_validate]
packages/flutter/src/widgets/form.dart 203:12 validate
packages/flutter/src/material/ink_well.dart 772:19 [_handleTap]
packages/flutter/src/material/ink_well.dart 855:36 <fn>
packages/flutter/src/gestures/recognizer.dart 182:24 invokeCallbackpackages/flutter/src/gestures/tap.dart 522:11 handleTapUp
packages/flutter/src/gestures/tap.dart 282:5 [_checkUp]
packages/flutter/src/gestures/tap.dart 254:7 acceptGesture
packages/flutter/src/gestures/arena.dart 156:12 sweep
packages/flutter/src/gestures/binding.dart 222:20 handleEvent
packages/flutter/src/gestures/binding.dart 198:14 dispatchEvent
packages/flutter/src/gestures/binding.dart 156:7
[_handlePointerEvent]
packages/flutter/src/gestures/binding.dart 102:7
[_flushPointerEventQueue]
packages/flutter/src/gestures/binding.dart 86:7
[_handlePointerDataPacket]
C:/b/s/w/ir/cache/builder/src/out/host_debug/flutter_web_sdk/lib/_engine/engine/window.dart 592:13 _invoke1
C:/b/s/w/ir/cache/builder/src/out/host_debug/flutter_web_sdk/lib/_engine/engine/window.dart 238:5
invokeOnPointerDataPacket
C:/b/s/w/ir/cache/builder/src/out/host_debug/flutter_web_sdk/lib/_engine/engine/pointer_binding.dart 129:14
[_onPointerData]
C:/b/s/w/ir/cache/builder/src/out/host_debug/flutter_web_sdk/lib/_engine/engine/pointer_binding.dart 479:16 <fn>
C:/b/s/w/ir/cache/builder/src/out/host_debug/flutter_web_sdk/lib/_engine/engine/pointer_binding.dart 440:21 <fn>
C:/b/s/w/ir/cache/builder/src/out/host_debug/flutter_web_sdk/lib/_engine/engine/pointer_binding.dart 210:16 <fn>
Handler: "onTap"
Recognizer:
TapGestureRecognizer#f9bab
I'm running the application on flutter web. Here is my flutter doctor output:
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel beta, 1.18.0-11.1.pre, on Microsoft Windows [Version 10.0.18362.836], locale en-SG)
[√] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
[√] Chrome - develop for the web
[√] Android Studio (version 3.6)
[!] IntelliJ IDEA Ultimate Edition (version 2019.2)
X Flutter plugin not installed; this adds Flutter specific functionality.
X Dart plugin not installed; this adds Dart specific functionality.
[√] VS Code (version 1.45.1)
[√] Connected device (2 available)
! Doctor found issues in 1 category.
My DropdownbuttonFormField code:
class RegistrationFormComponent extends StatelessWidget {
static GlobalKey<FormState> _formKey;
RegisterBloc registerBloc;
int stage;
@override
Widget build(BuildContext context) {
_formKey = GlobalKey<FormState>(debugLabel: "register_key");
int stage = registerBloc.state.stage;
switch (stage) {
case 0:
return _form1();
case 1:
return _form2();
default:
return _form3();
}
}
Widget _form3() {
return Form(
key: _formKey,
child: Column(
children: <Widget>[
SizedBox(height: 30.0),
_buildPositionTF(),
],
),
);
}
Widget _buildPositionTF() {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
'Position: ',
style: TextStyle(
fontFamily: 'OpenSans',
fontSize: 16.0,
color: Colors.black87,
),
),
SizedBox(height: 10.0),
Container(
height: 60.0,
child: DropdownButtonFormField<String>(
isExpanded: true,
value: (registerBloc.state as RegisterProgress).position,
hint: Text(
'Select your position',
),
validator:
RequiredValidator(errorText: "Please select your position."),
icon: Icon(Icons.arrow_downward),
iconSize: 24,
onChanged: (newValue) => registerBloc.add(RegisterSavePositionEvent(
newValue)), // add select Position event
items: ((registerBloc.state as RegisterProgress).positions)
.map<DropdownMenuItem<String>>((Object value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value as String),
);
}).toList(),
),
),
],
);
}
Here is my submit button code:
Widget _buildNextBtn(RegisterBloc registerBloc) {
return HandCursor(
child: Container(
padding: EdgeInsets.symmetric(vertical: 25.0),
width: double.infinity,
child: RaisedButton(
elevation: 5.0,
onPressed: () {
print("next button pressed");
if (!(RegistrationFormComponent.getFormKey()
as GlobalKey<FormState>)
.currentState
.validate()) {
return;
}
...
I'm not sure whether this is the validator package issue or flutter issue or my own issure. Could you please help me?
When I try to evaluate a string for minimum length and maximum length validator it would take the trailing spaces at the start and end of the string as well into the count. It would be good if it trims the string before it is being evaluated so that we get the desired result. For example, if I set the minimum length of the string to be 3 characters and if I enter 3 whitespaces or white space at the start, character or number in the middle and again a white space at the end, in both scenarios it evaluates to True, in such scenarios it should get evaluated to false.
Hope this issue gets fixed at the earliest.
Could you improve the doc so we know all the validators and how to use them? :)
validating the string [email protected]
is failing, because of case sensitive.
I live in Mexico and want the hint text from country picker to be in spanish, you should do something to make it easy! :)
After flutter 2.0 update every package needs to be migrated to null safety.
Hello,
This is a good package to validate text, thanks.
We would ask if there is a way to customize the error messages: let's say if we want to put or longer messages, or if we want to localizate them in other languages.
Is it hard to do?
Thanks
Hello! I wrote a custom validator which I add to a MultiValidator:
class NotNullOrEmptyValidator<T> extends FieldValidator<T> {
NotNullOrEmptyValidator({required String errorText}) : super(errorText);
@override
bool isValid(T value) {
return value != null;
}
}
I use it like:
validators.add(NotNullOrEmptyValidator<File>(errorText: 'Required'));
So basically I just checked if the given file is null or not.
When validation occurs, I get this error back:
type 'Null' is not a subtype of type 'File' of 'value'
When the exception was thrown, this was the stack:
#0 FieldValidator.call (package:form_field_validator/form_field_validator.dart)
#1 MultiValidator.isValid (package:form_field_validator/form_field_validator.dart:170:21)
#2 MultiValidator.call (package:form_field_validator/form_field_validator.dart:180:12)
#3 FormFieldState._validate (package:flutter/src/widgets/form.dart:468:37)
#4 FormFieldState.validate.<anonymous closure> (package:flutter/src/widgets/form.dart:461:7)
I also tried to change my logic and not use File, but just a simple String (path of the file), but still I get the same error, but with this message: type 'Null' is not a subtype of type 'String' of 'value'
use the line below so the function is used instead of the error text being returned directly
_errorText = validator.call(value)!;
INSTEAD OF
_errorText = validator.errorText;
REFERING TO
class MultiValidator extends FieldValidator<String?> {
final List validators;
static String _errorText = '';
MultiValidator(this.validators) : super(_errorText);
@OverRide
bool isValid(value) {
for (FieldValidator validator in validators) {
if (validator.call(value) != null) {
_errorText = validator.errorText;
return false;
}
}
return true;
}
@OverRide
String? call(dynamic value) {
return isValid(value) ? null : _errorText;
}
}
Thank you for creating such a good package.
Is there any control on the position of the error message, so that we can change it and customize its position ?
the named parameter 'onChange' isn't defined for TextFormField .
its for TextField and 'Validator' it is not defined for TextFiled
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/.*/
.
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.