string-or-false * Return an error-message (cannot contain html markup), * if $str longer than $maxAllowedLen * or if $str contains spaces and $spacesAllowed===false. * Returns false, if there is no error-message to report. */ function stringInvalidMsg( $str, $maxAllowedLen, $spacesAllowed = true ) { $problemsSoFar = ""; $excessLen = (strlen($str) - $maxAllowedLen); if ($excessLen > 0) { $problemsSoFar .= "Can't be more than $maxAllowedLen characters long (it is " . pluralize($excessLen, "character") . " too long). "; } if (!$spacesAllowed && preg_match('/\p{Z}/',$str)) { $problemsSoFar .= "Cannot contain any space characters. "; } return ($problemsSoFar==="") ? false : $problemsSoFar; } /* internal helper for allErrorMsgs */ function addStringInvalidMsgToArray( $formInfo, $key, $maxLength, $spacesAllowed, $theMsgArray ) { $errMsg = stringInvalidMsg(safeLookup($formInfo,$key,""), $maxLength, $spacesAllowed); if ($errMsg !== false) { $theMsgArray[$key] = $errMsg; } return $theMsgArray; } /* allErrorMsgs : string[] -> string[] * Given form information for `form0-validated.php`, * return an array of all the error-messages, * indexed by the offending field. */ function allErrorMsgs( $formInfo ) { $allErrors = array(); $allErrors = addStringInvalidMsgToArray( $formInfo, 'quest', 20, true, $allErrors ); $allErrors = addStringInvalidMsgToArray( $formInfo, 'favorite-color', 15, false, $allErrors ); $allErrors = addStringInvalidMsgToArray( $formInfo, 'msg', 1000, true, $allErrors ); if (array_key_exists('is-happy',$formInfo) && $formInfo['is-happy'] !== "yep") { $allErrors['is-happy'] = "Your happiness-level is wrong. (Go away, form tamperer!)"; } return $allErrors; } ?>