Reputation: 15
I have 3 fields (name, password, email). I want to check if they are valid or not. I wrote the following
public boolean isValidInput() {
if(name.isValid()){
return false;
}
if(password.isInValid()){
return false;
}
if(email.isInValid()){
return false;
}
return true;
}
so this will give me a single invalid. But what to do if I want to show the invalids all at the same time?
Upvotes: 0
Views: 54
Reputation: 53006
You could simply return an integer from the function like this
public int isValidInput() {
if(name.isValid()){
return 1;
}
if(password.isInValid()){
return 2;
}
if(email.isInValid()){
return 3;
}
return 0;
}
and then check the integer to find out which one failed!
It would be better of course to define static final
int
s with the names of the errors to make the code more readable and robust.
Upvotes: 0
Reputation: 5711
There are multiple ways you can handle this. But each of them need a change in the caller to handle these cases.
isValidInput()
if the list is not empty then throw an exception with the list of errors.There are still a lot of other ways to handle this. It all depends on what suits you the best.
I would say, try some of them and see how it goes.
Upvotes: 1