Validation chain

Tags
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.

  1. $errorMessage = '';
  2. Get input.
  3. If error 1 found:
  4.   // Something is wrong, set error message.
  5.   $errorMessage = 'Sorry, ...';
  6. If $errorMessage == '' then:
  7.   // No errors so far. Next check.
  8.   If error 2 found:
  9.     $errorMessage = 'Sorry, ...';
  10. If $errorMessage == '' then:
  11.   // No errors so far. Next check.
  12.   If error 3 found:
  13.     $errorMessage = 'Sorry, ...';
  14. If $errorMessage == '' then:
  15.   // No errors so far. Next check.
  16.   If error 4 found:
  17.     $errorMessage = 'Sorry, ...';
  18. If $errorMessage == '' then:
  19.   // No errors! Processing.
  20.   Process input.
  21.  
  22. Later...
  23. if ($errorMessage != '') {
  24.   print $errorMessage;
  25. else {
  26.   print output from processing;
  27. }
Where referenced