Summary
Test some input, making an error message if needed. If that's OK, run another test. If it's still OK, do another.
Situation
You want to validate some data. You have several rules the data should pass.
Action
Create an error message variable. Use it as a flag as well. Before each test, check to make sure there have been no errors.
- $errorMessage = '';
- Get input.
- If error 1 found:
- // Something is wrong, set error message.
- $errorMessage = 'Sorry, ...';
- If $errorMessage == '' then:
- // No errors so far. Next check.
- If error 2 found:
- $errorMessage = 'Sorry, ...';
- If $errorMessage == '' then:
- // No errors so far. Next check.
- If error 3 found:
- $errorMessage = 'Sorry, ...';
- If $errorMessage == '' then:
- // No errors so far. Next check.
- If error 4 found:
- $errorMessage = 'Sorry, ...';
- If $errorMessage == '' then:
- // No errors! Processing.
- Process input.
- Later...
- if ($errorMessage != '') {
- print $errorMessage;
- else {
- print output from processing;
- }
Where referenced