Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use BLOC pattern to validate 'Confirm Password' text field? #1

Open
jonahfang opened this issue Sep 26, 2018 · 2 comments
Open

Comments

@jonahfang
Copy link

I am using a Stream/Observable to validate 'Password' text field like final _emailController = BehaviorSubject(); Stream get email => _emailController.stream.transform(validateEmail); where valdiateEmail is a StreamTransformer<String,String> to validate whether the password length is at least 6. I am trying to use the similar idea for 'Confirm Password' but the validate function would take two inputs: password, confirmPassword; not sure how to handle this using StreamTransformer.

@boeledi
Copy link

boeledi commented Dec 2, 2018

The easiest way is to use an Observable.combineLatestXXX(), combined with a stream.transform(...).doOnData(...) as follows:

Stream<bool> get registerValid => Observable.combineLatest3(
                                      email, 
                                      password, 
                                      confirmPassword, 
                                      (e, p, c) => (0 == p.compareTo(c))
                                    );

Stream<String> get confirmPassword => 
  _passwordConfirmController.stream.transform(validatePassword)
    .doOnData((String c){
      // If the password is accepted (after validation of the rules)
      // we need to ensure both password and retyped password match
      if (0 != _passwordController.value.compareTo(c)){
        // If they do not match, add an error
        _passwordConfirmController.addError("No Match");
      }
    });

I just wrote an article on BLoC Use cases which contains a full explanation on this topic. The article may be found here

Hope this will help.

@aniltc-rails
Copy link

hi
I have multiple textfields, in this case how to use combineLatest. let's say I have 10 textfield , its dyanmically created by the user in UI.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants