flutter-form-builder-ecosystem / flutter-form-builder-ecosystem/form_builder_validators
FormBuilderValidators.equal() do not update value to compare
- Dominant language
- Dart
- Stars
- 67
- Forks
- 106
- Avg merge
- 2d 21h
- Merged PRs (30d)
- 1
Description
### Discussed in https://github.com/flutter-form-builder-ecosystem/form_builder_validators/discussions/61
Originally posted by **orbiteleven** August 30, 2023
Using `form_builder_validators` with `flutter_form_builder` and I have what I think should be a pretty common password+confirm password form, but I'm a bit confused. This is what I have so far:
```dart
FormBuilderTextField(
name: 'password',
decoration: InputDecoration(labelText: 'Password'),
obscureText: true,
validator: FormBuilderValidators.compose([
FormBuilderValidators.required(),
FormBuilderValidators.minLength(6)
])),
FormBuilderTextField(
name: 'confirmPassword',
decoration: InputDecoration(labelText: 'Confirm Password'),
obscureText: true,
validator: FormBuilderValidators.compose([
FormBuilderValidators.required(),
FormBuilderValidators.minLength(6),
(value) {
print(
"value: $value matches ${_formKey.currentState?.instantValue['password']}");
return null;
},
FormBuilderValidators.equal(
_formKey.currentState?.instantValue['password'] ?? '',
errorText: 'Passwords do not match')
])),
```
the "custom" validator works fine, but `FormBuilderValidators.equal()` throws an error. What am I doing wrong?
For some reason, FormBuilderValidators.equal validator don't update value to compare with fieldValue
A nice way to show the error, is create a validate method with print:
```dart
FormFieldValidator equal(
Object? value, {
String? errorText,
}) =>
(T? valueCandidate) {
print(
'value: $value valueCandidate: $valueCandidate'
);
// Value is null and valueCandidate the form field value, when update field value
return valueCandidate != value ? errorText : null;
};
```
Contributor guide
Assessment
This issue has not been assessed yet.