Skip to content

Instantly share code, notes, and snippets.

@boeledi
Last active March 27, 2023 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boeledi/6e10cea357472049d6079cb8ec7b12aa to your computer and use it in GitHub Desktop.
Save boeledi/6e10cea357472049d6079cb8ec7b12aa to your computer and use it in GitHub Desktop.
class RegistrationFormBloc extends Object with EmailValidator, PasswordValidator implements BlocBase {
final BehaviorSubject<String> _emailController = BehaviorSubject<String>();
final BehaviorSubject<String> _passwordController = BehaviorSubject<String>();
final BehaviorSubject<String> _passwordConfirmController = BehaviorSubject<String>();
//
// Inputs
//
Function(String) get onEmailChanged => _emailController.sink.add;
Function(String) get onPasswordChanged => _passwordController.sink.add;
Function(String) get onRetypePasswordChanged => _passwordConfirmController.sink.add;
//
// Validators
//
Stream<String> get email => _emailController.stream.transform(validateEmail);
Stream<String> get password => _passwordController.stream.transform(validatePassword);
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");
}
});
//
// Registration button
Stream<bool> get registerValid => Rx.combineLatest3<String, String, String, bool>(
email,
password,
confirmPassword,
(e, p, c) => true
);
@override
void dispose() {
_emailController.close();
_passwordController.close();
_passwordConfirmController.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment