Skip to content

FormBuilderValidators.equal() do not update value to compare #63

Description

@deandreamatias

Discussed in #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:

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:

FormFieldValidator<T> equal<T>(
  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;
    };

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghelp wantedExtra attention is needed

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    Status
    Ready

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions