Skip to content

Commit e8410b3

Browse files
docs: add validate form example
1 parent 7541ae5 commit e8410b3

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,51 @@ See [pub.dev example tab](https://pub.dev/packages/flutter_form_builder/example)
9999

100100
### Specific uses
101101

102+
#### Validate and get values
103+
104+
```dart
105+
final _formKey = GlobalKey<FormBuilderState>();
106+
107+
FormBuilder(
108+
key: _formKey,
109+
child: Column(
110+
children: [
111+
FormBuilderTextField(
112+
key: _emailFieldKey,
113+
name: 'email',
114+
decoration: const InputDecoration(labelText: 'Email'),
115+
validator: FormBuilderValidators.compose([
116+
FormBuilderValidators.required(),
117+
FormBuilderValidators.email(),
118+
]),
119+
),
120+
const SizedBox(height: 10),
121+
FormBuilderTextField(
122+
name: 'password',
123+
decoration: const InputDecoration(labelText: 'Password'),
124+
obscureText: true,
125+
validator: FormBuilderValidators.compose([
126+
FormBuilderValidators.required(),
127+
]),
128+
),
129+
MaterialButton(
130+
color: Theme.of(context).colorScheme.secondary,
131+
onPressed: () {
132+
// Validate and save the form values
133+
_formKey.currentState?.saveAndValidate();
134+
debugPrint(_formKey.currentState?.value.toString());
135+
136+
// On another side, can access all field values without saving form with instantValues
137+
_formKey.currentState?.validate();
138+
debugPrint(_formKey.currentState?.instantValue.toString());
139+
},
140+
child: const Text('Login'),
141+
)
142+
],
143+
),
144+
),
145+
```
146+
102147
#### Building your own custom field
103148

104149
To build your own field within a `FormBuilder`, we use `FormBuilderField` which will require that you define your own field.

0 commit comments

Comments
 (0)