themisir / form-validator

Simplest form validation for flutter form widgets
https://pub.dev/packages/form_validator
MIT License
78 stars 41 forks source link

Passing the data in the text editing controller to the validation builder custom extension #43

Closed Turkmen48 closed 2 years ago

Turkmen48 commented 2 years ago

Is your feature request related to a problem? Please describe. I dont know if is possible but i tried to Passing the data in the text editing controller to the validation builder (my custom validation builder extension) but it doesnt work.

Describe the solution you'd like I want to send the string data I got from the password controller to custom Validator Builder. When I tested it I noticed that, string data in the text editing controller does not reach the validation builder. But then when I try to access it with the regular button, the text editing controller works fine.

Additional context

Text Form Field codes:

child: TextFormField(
                    controller: _repeatPassController,
                    validator: ValidationBuilder(
                            requiredMessage: "Bu alan gereklidir!")
                        .repeatPassword(_passController.text)
                        .build(),
                    obscureText: true,
                    decoration: InputDecoration(
                        border: OutlineInputBorder(),
                        labelText: 'Tekrar Şifre',
                        hintText: 'Güvenli bir şifre giriniz'),
                  ),

This part of my codes is kept in another file called consts.dart.

extension CustomValidationBuilderRepeatPassword on ValidationBuilder {
  ///check if password repeated
  ///if password is not repeated return error message
  repeatPassword(String password) => add((value) {
        print("password: $password");
        if (value == null || value.isEmpty) {
          return "Bu alan gereklidir";
        }

        if (value != password) {
          print(value + " value");
          print(password + " password");
          return 'Şifreler eşleşmiyor!';
        }
        return null;
      });
}
themisir commented 2 years ago

I think I know what's going on. String is immutable object. So, when you do repeatPassword(controller.value) it passes the actual value instead of reference to the value, meaning it will check password against initial value (probably empty string).

To fix it I would suggest passing TextEdigintController instead like:

extension CustomValidationBuilderRepeatPassword on ValidationBuilder {
  ///check if password repeated
  ///if password is not repeated return error message
  repeatPassword(TextEditingController password) => add((value) {
        if (value == null || value.isEmpty) {
          return "Bu alan gereklidir";
        }

        if (value != password.value) {
          return 'Şifreler eşleşmiyor!';
        }
        return null;
      });
}
Turkmen48 commented 2 years ago

thank you so much. It worked.