For each input to check:
- Make an error message variable for that input.
- Use the single-field validation pattern for each input.
Combine the error messages for each field.
You have several pieces of user input, from GET (the URL), or POST (a form). You want to check they're all OK.
Here's code for validating several fields about a slug:
- // getParamFromPost() gets a param from POST data.
- $name = getParamFromPost('name');
- // checkName() returns an error message, or MT if $name is OK.
- $nameErrorMessage = checkName($name);
- $gender = getParamFromPost('gender');
- $genderErrorMessage = checkGender($gender);
- $weight = getParamFromPost('weight');
- $weightErrorMessage = checkWeight($weight);
- $yuckiness = getParamFromPost('yuckiness');
- $yuckinessErrorMessage = checkYuckiness($yuckiness);
Write a function to check each field. The function returns an error message, or an MT string if everything's OK. So checkGender('q')
might return "Sorry, gender 'q' is unknown.<br>\n"
, while checkGender('f')
returns ''. Notice the br
tag. That means that error messages can be combine, and they'll all be on separate lines.
Combine the error messages into one:
- $errorMessages = $nameErrorMessage . $genderErrorMessage
- . $weightErrorMessage . $yuckinessErrorMessage;
If the component error messages are all MT, then $errorMessages
will be MT as well.
Later, test $errorMessages
. It it's not MT, output it, in an HTML container.
- if ($errorMessages != '') {
- ?>
- <div class="error-message-container">
- <?= $errorMessages ?>
- </div>
- <?php
- }
- else {
- // Processing.