* @since 15 Jul 2005 01:39:14
*/
public function printErrorMessages($above='', $below='', $print_gotohash_js=false, $hash='sc-msg-formvalidator')
{
$app =& App::getInstance();
if ($this->anyErrors()) {
?>
anyErrors($form_name))) {
if (isset($marker)) {
echo $marker;
} else {
switch ($type) {
case MSG_ERR:
default:
echo $this->getParam('error');
break;
case MSG_WARNING:
echo $this->getParam('warning');
break;
case MSG_NOTICE:
echo $this->getParam('notice');
break;
case MSG_SUCCESS:
echo $this->getParam('success');
break;
}
}
}
}
/**
* Ensure the length of string is non-zero.
*
* @param string $form_name the name of the incoming form variable
* @param string $msg the message to display on error
*
* @return bool true if form is not empty, false otherwise.
*/
public function notEmpty($form_name, $msg='')
{
if (!Validator::isEmpty(getFormData($form_name))) {
return true;
} else {
$this->addError($form_name, $msg);
return false;
}
}
/*
* We were using the isEmpty method *wrong* all these years and should have been using notEmpty.
* But the fact is the only use is to ensure a value is not empty, so this function simply becomes
* an alias of the one-true notEmpty() function.
* @since 03 Jun 2006 22:56:46
*/
public function isEmpty($form_name, $msg='')
{
return $this->notEmpty($form_name, $msg);
}
/**
* Check whether input is a string.
*
* @param string $form_name the name of the incoming form variable
* @param string $msg the message to display on error
*
* @return bool true if form is a string, false otherwise.
*/
public function isString($form_name, $msg='')
{
if (Validator::isString(getFormData($form_name))) {
return true;
} else {
$this->addError($form_name, $msg);
return false;
}
}
/**
* Check whether input is a number. Allows negative numbers.
*
* @param string $form_name the name of the incoming form variable
* @param string $msg the message to display on error
*
* @return bool true if no errors found, false otherwise
*/
public function isNumber($form_name, $msg='')
{
if (Validator::isNumber(getFormData($form_name))) {
return true;
} else {
$this->addError($form_name, $msg);
return false;
}
}
/**
* addError if input is NOT an integer. Don't just use is_int() because the
* data coming from the user is *really* a string.
*
* @param string $form_name the name of the incoming form variable
* @param string $msg the message to display on error
*
* @return bool true if value is an integer
*/
public function isInteger($form_name, $msg='', $negative_ok=false)
{
if (Validator::isInteger(getFormData($form_name), $negative_ok)) {
return true;
} else {
$this->addError($form_name, $msg);
return false;
}
}
/**
* Check whether input is a float. Don't just use is_float() because the
* data coming from the user is *really* a string. Integers will also
* pass this test.
*
* @param string $form_name the name of the incoming form variable
* @param string $msg the message to display on error
*
* @return bool true if value is a float
*/
public function isFloat($form_name, $msg='', $negative_ok=false)
{
if (Validator::isFloat(getFormData($form_name), $negative_ok)) {
return true;
} else {
$this->addError($form_name, $msg);
return false;
}
}
/**
* Check whether input is an array.
*
* @param string $form_name the name of the incoming form variable
* @param string $msg the message to display on error
*
* @return bool true if value is a float
*/
public function isArray($form_name, $msg='')
{
if (Validator::isArray(getFormData($form_name))) {
return true;
} else {
$this->addError($form_name, $msg);
return false;
}
}
/**
* Check whether input matches the specified perl regular expression
* pattern.
*
* @param string $form_name The name of the incoming form variable
* @param int $regex Perl regex that the string must match
* @param bool $valid_on_match Set to true to be valid if match, or false to be valid if the match fails.
* @param string $msg The message to display on error
*
* @return bool true if value passes regex test
*/
public function checkRegex($form_name, $regex, $valid_on_match=true, $msg='')
{
if (Validator::checkRegex(getFormData($form_name), $regex, $valid_on_match)) {
return true;
} else {
$this->addError($form_name, $msg);
return false;
}
}
/**
* Tests if the string length is between specified values. Whitespace excluded for min.
*
* @param string $form_name the name of the incoming form variable
* @param int $min minimum length of string, inclusive
* @param int $max maximum length of string, inclusive
* @param string $msg the message to display on error
*
* @return bool true if string length is within given boundaries
*/
public function stringLength($form_name, $min, $max, $msg='')
{
if (Validator::stringLength(getFormData($form_name), $min, $max)) {
return true;
} else {
$this->addError($form_name, $msg);
return false;
}
}
/**
* Check whether input is within a valid numeric range.
*
* @param string $form_name the name of the incoming form variable
* @param int $min minimum value of number, inclusive
* @param int $max maximum value of number, inclusive
* @param string $msg the message to display on error
*
* @return bool true if no errors found, false otherwise
*/
public function numericRange($form_name, $min, $max, $msg='')
{
if (Validator::numericRange(getFormData($form_name), $min, $max)) {
return true;
} else {
$this->addError($form_name, $msg);
return false;
}
}
/**
* Validates an email address based on the recommendations in RFC 3696.
* Is more loose than restrictive, to allow the many valid variants of
* email addresses while catching the most common mistakes.
* http://www.faqs.org/rfcs/rfc822.html
* http://www.faqs.org/rfcs/rfc2822.html
* http://www.faqs.org/rfcs/rfc3696.html
* http://www.faqs.org/rfcs/rfc1035.html
*
* @access public
* @param string $form_name The name of the incoming form variable.
* @return bool Validity of address.
* @author Quinn Comendant
*/
public function validateEmail($form_name, $strict=false)
{
$app =& App::getInstance();
$email = getFormData($form_name);
if ('' == trim($email)) {
// No email address provided, and that's okay.
return true;
}
// Validator::validateEmail() returns a value that relates to the Validate::EMAIL_* constants (defined in Validator.inc.php).
switch (Validator::validateEmail($email, $strict)) {
case Validator::EMAIL_REGEX_FAIL:
// Failed regex match.
$this->addError($form_name, sprintf(_("The email address %s is formatted incorrectly."), oTxt($email)));
$app->logMsg(sprintf('The email address %s is not valid.', oTxt($email)), LOG_DEBUG, __FILE__, __LINE__);
return false;
case Validator::EMAIL_LENGTH_FAIL :
// Failed length requirements.
$this->addError($form_name, sprintf(_("The email address %s is too long (email addresses must have fewer than 256 characters)."), oTxt($email)));
$app->logMsg(sprintf('The email address %s must contain less than 256 characters.', oTxt($email)), LOG_DEBUG, __FILE__, __LINE__);
return false;
case Validator::EMAIL_MX_FAIL :
// Failed MX record test.
$this->addError($form_name, sprintf(_("The email address %s does not have a valid domain name"), oTxt($email)));
$app->logMsg(sprintf('The email address %s does not have a valid domain name.', oTxt($email)), LOG_INFO, __FILE__, __LINE__);
return false;
case Validator::EMAIL_SUCCESS :
default :
return true;
}
}
/**
* Check whether input is a valid phone number. Notice: it is now set
* to allow characters like - or () or + so people can type in a phone
* number that looks like: +1 (530) 555-1212
*
* @param string $form_name the name of the incoming form variable
*
* @return bool true if no errors found, false otherwise
*/
public function validatePhone($form_name)
{
$app =& App::getInstance();
$phone = getFormData($form_name);
// Validator::validateEmail() returns a value that relates to the Validate::PHONE_* constants (defined in Validator.inc.php).
switch (Validator::validatePhone($phone)) {
case Validator::PHONE_REGEX_FAIL:
// Failed regex match.
$this->addError($form_name, sprintf(_("The phone number %s is not valid."), oTxt($phone)));
$app->logMsg(sprintf('The phone number %s is not valid.', oTxt($phone)), LOG_DEBUG, __FILE__, __LINE__);
return false;
case Validator::PHONE_LENGTH_FAIL :
// Failed length requirements.
$this->addError($form_name, sprintf(_("The phone number %s is too long (phone number must have fewer than 25 characters)."), oTxt($phone)));
$app->logMsg(sprintf('The phone number %s must contain less than 256 characters.', oTxt($phone)), LOG_DEBUG, __FILE__, __LINE__);
return false;
case Validator::PHONE_SUCCESS :
default :
return true;
}
}
/**
* Verifies that date can be processed by the strtotime function.
*
* @param string $form_name the name of the incoming form variable
* @param string $msg the message to display on error
*
* @return bool true if no errors found, false otherwise
*/
public function validateStrDate($form_name, $msg='')
{
$app =& App::getInstance();
if (Validator::validateStrDate(getFormData($form_name, ''))) {
return true;
} else {
$this->addError($form_name, $msg);
$app->logMsg(sprintf('The string date %s is not valid.', getFormData($form_name)), LOG_DEBUG, __FILE__, __LINE__);
return false;
}
}
/**
* Verifies credit card number using the Luhn (mod 10) algorithm.
* http://en.wikipedia.org/wiki/Luhn_algorithm
*
* @param string $form_name The name of the incoming form variable.
* @param string $cc_type Optional, card type to do specific checks. One of the Validator::CC_TYPE_* constants.
*
* @return bool true if no errors found, false otherwise
*/
public function validateCCNumber($form_name, $cc_type=null)
{
$cc_num = getFormData($form_name);
if (Validator::validateCCNumber($cc_num, $cc_type)) {
return true;
} else {
$this->addError($form_name, sprintf(_("The credit card number you entered is not valid. Please check the number and try again."), $cc_num));
return false;
}
}
/**
* Check whether a file was selected for uploading. If file is missing, it's an error.
*
* @param string $form_name the name of the incoming form variable
* @param string $msg the message to display on error
*
* @return bool true if no errors found, false otherwise
*/
public function fileUploaded($form_name, $msg='')
{
if (Validator::fileUploaded($form_name)) {
return true;
} else {
$this->addError($form_name, $msg);
return false;
}
}
/**
* Check whether a file was selected for uploading. If file is missing, it's an error.
*
* @param string $form_name the name of the incoming form variable
* @param string $msg the message to display on error
*
* @return bool true if no errors found, false otherwise
*/
public function fileUploadSize($form_name, $msg='')
{
if (Validator::fileUploadSize($form_name)) {
return true;
} else {
$msg = '' == $msg ? sprintf(_("Maximum filesize exceeded. Got %s, but limit is %s."), humanFileSize($_SERVER['CONTENT_LENGTH']), humanFileSize(phpIniGetBytes('upload_max_filesize'))) : $msg;
$this->addError($form_name, $msg);
return false;
}
}
} // THE END