'Validating text form with checkboxes in flutter
I am using bloc and rxdart as state management. I also do the validation of forms with that. And this works fine when I want to validate e.g. multiple text fields. But what I want to do now is to validate a text field and checkboxes. So the textfield should have a value and one of the checkboxes should be ticked by the user before storing the values in the database.
The stream of the text field and its validator.
Stream<String> get newsText => _newsText.stream.transform(validateNewsText);
handleData: (newsText, sink) {
if (newsText != null) {
if (newsText.length >= 10 && newsText.length <= 200) {
sink.add(newsText.trim());
} else {
if (newsText.length < 10) {
sink.addError("Minimum 10 characters");
} else {
sink.addError("Maximum 25 characters");
}
}
}
});
Now the part which I am unsure with and which doesnt work. I created stream for the list, which holds the values generated by ticking the check boxes. And I also made a validator for it.
Stream<List<String>> get getUserRoleList => _userRoleList.stream.transform(validateUserRoleList);
StreamTransformer<List<String>, List<String>>.fromHandlers(
handleData: (userRoleList, sink) {
if (userRoleList != null) {
sink.add(userRoleList);
} else {
sink.addError("Es muss mindestens eine Rolle ausgewählt werden");
}
});
Finally I use CombineLatestStream to get back a bool depending on the availability of the values
Stream<bool> get isValid => CombineLatestStream.combine2(newsText, getUserRoleList, (a, b) => true);
Then I want to disable the button if there is no value in the combined stream. But somehow this does not work but it already does when I want to validate just text fields.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
