Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
F
Front-End
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
abdullh.alsoleman
Front-End
Commits
e7c90057
Unverified
Commit
e7c90057
authored
Feb 19, 2020
by
Tjong Anthony
Committed by
GitHub
Feb 19, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[Form] Add is valid to FormState (#48948)
parent
4c5aef63
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
109 additions
and
0 deletions
+109
-0
form.dart
packages/flutter/lib/src/widgets/form.dart
+15
-0
form_test.dart
packages/flutter/test/widgets/form_test.dart
+94
-0
No files found.
packages/flutter/lib/src/widgets/form.dart
View file @
e7c90057
...
...
@@ -349,6 +349,16 @@ class FormFieldState<T> extends State<FormField<T>> {
/// True if this field has any validation errors.
bool
get
hasError
=>
_errorText
!=
null
;
/// True if the current value is valid.
///
/// This will not set [errorText] or [hasError] and it will not update
/// error display.
///
/// See also:
///
/// * [validate], which may update [errorText] and [hasError].
bool
get
isValid
=>
widget
.
validator
?.
call
(
_value
)
==
null
;
/// Calls the [FormField]'s onSaved method with the current value.
void
save
()
{
if
(
widget
.
onSaved
!=
null
)
...
...
@@ -365,6 +375,11 @@ class FormFieldState<T> extends State<FormField<T>> {
/// Calls [FormField.validator] to set the [errorText]. Returns true if there
/// were no errors.
///
/// See also:
///
/// * [isValid], which passively gets the validity without setting
/// [errorText] or [hasError].
bool
validate
()
{
setState
(()
{
_validate
();
...
...
packages/flutter/test/widgets/form_test.dart
View file @
e7c90057
...
...
@@ -137,6 +137,100 @@ void main() {
await
checkErrorText
(
''
);
});
testWidgets
(
'isValid returns true when a field is valid'
,
(
WidgetTester
tester
)
async
{
final
GlobalKey
<
FormFieldState
<
String
>>
fieldKey1
=
GlobalKey
<
FormFieldState
<
String
>>();
final
GlobalKey
<
FormFieldState
<
String
>>
fieldKey2
=
GlobalKey
<
FormFieldState
<
String
>>();
const
String
validString
=
'Valid string'
;
String
validator
(
String
s
)
=>
s
==
validString
?
null
:
'Error text'
;
Widget
builder
()
{
return
MaterialApp
(
home:
MediaQuery
(
data:
const
MediaQueryData
(
devicePixelRatio:
1.0
),
child:
Directionality
(
textDirection:
TextDirection
.
ltr
,
child:
Center
(
child:
Material
(
child:
Form
(
child:
ListView
(
children:
<
Widget
>[
TextFormField
(
key:
fieldKey1
,
initialValue:
validString
,
validator:
validator
,
autovalidate:
true
),
TextFormField
(
key:
fieldKey2
,
initialValue:
validString
,
validator:
validator
,
autovalidate:
true
),
],
),
),
),
),
),
),
);
}
await
tester
.
pumpWidget
(
builder
());
expect
(
fieldKey1
.
currentState
.
isValid
,
isTrue
);
expect
(
fieldKey2
.
currentState
.
isValid
,
isTrue
);
});
testWidgets
(
'isValid returns false when the field is invalid and does not change error display'
,
(
WidgetTester
tester
)
async
{
final
GlobalKey
<
FormFieldState
<
String
>>
fieldKey1
=
GlobalKey
<
FormFieldState
<
String
>>();
final
GlobalKey
<
FormFieldState
<
String
>>
fieldKey2
=
GlobalKey
<
FormFieldState
<
String
>>();
const
String
validString
=
'Valid string'
;
String
validator
(
String
s
)
=>
s
==
validString
?
null
:
'Error text'
;
Widget
builder
()
{
return
MaterialApp
(
home:
MediaQuery
(
data:
const
MediaQueryData
(
devicePixelRatio:
1.0
),
child:
Directionality
(
textDirection:
TextDirection
.
ltr
,
child:
Center
(
child:
Material
(
child:
Form
(
child:
ListView
(
children:
<
Widget
>[
TextFormField
(
key:
fieldKey1
,
initialValue:
validString
,
validator:
validator
,
autovalidate:
false
,
),
TextFormField
(
key:
fieldKey2
,
initialValue:
''
,
validator:
validator
,
autovalidate:
false
,
),
],
),
),
),
),
),
),
);
}
await
tester
.
pumpWidget
(
builder
());
expect
(
fieldKey1
.
currentState
.
isValid
,
isTrue
);
expect
(
fieldKey2
.
currentState
.
isValid
,
isFalse
);
expect
(
fieldKey2
.
currentState
.
hasError
,
isFalse
);
},
);
testWidgets
(
'Multiple TextFormFields communicate'
,
(
WidgetTester
tester
)
async
{
final
GlobalKey
<
FormState
>
formKey
=
GlobalKey
<
FormState
>();
final
GlobalKey
<
FormFieldState
<
String
>>
fieldKey
=
GlobalKey
<
FormFieldState
<
String
>>();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment