Checking input is one of the possible cases. Usually you want to report all errors in the form to the user in one pass, and not stop after the first and force them to click the button again and get only one error each time:
public boolean validateField(string userInput, string paramName) { bool valid; //do validation if (valid) { //updates UI to remove error indicator (if present) reportValid(paramName); } else { //updates UI to indicate a problem (color change, error icon, etc) reportInvalid(paramName); } } public boolean validateAllInput(...) { boolean valid = true; valid = valid & validateField(userInput1, paramName1); valid = valid & validateField(userInput2, paramName2); valid = valid & validateField(userInput3, paramName3); valid = valid & validateField(userInput4, paramName4); valid = valid & validateField(userInput5, paramName5); return valid; } public void onSubmit() { if (validateAllInput(...)) { //go to next page of wizard, update database, etc processUserInput(userInput1, userInput2, ... ); } } public void onInput1Changed() { validateField(input1.Text, paramName1); } public void onInput2Changed() { validateField(input2.Text, paramName2); } ...
Of course, you could trivially avoid the need to evaluate the short circuit in validateAllInput() by refactoring the logic if (valid) { reportValid() ... outside validateField() ; but then you will need to call the extracted code every time validateField () is called; with a minimum of 10 extra lines for method calls. As always, this is the case when compromise is best for you.
Dan Neely Jul 10 2018-12-12T00: 00Z
source share