Back to index

awl  0.53
Public Member Functions | Public Attributes
Validation Class Reference

List of all members.

Public Member Functions

 Validation ($func_name)
 

@-


 AddRule ($fieldname, $error_message, $function_name)
 Adds a validation rule for a specific field upon submission of the form.
 RenderJavascript ($prefix="")
 Returns the javascript for form validation using the rules.
 Validate ($object)
 Validates the form according to it's rules.
 not_empty ($field_string)
 Checks if a string is empty.
 selected ($field_string)
 Checks that a string is not empty or zero.
 positive_dollars ($field_string)
 Check that the given string is a positive dollar amount.
 positive_integer ($field_string)
 Check that the given string is a positive integer.
 valid_email_format ($field_string)
 Check that the given string is a valid email address.
 valid_date_format ($field_string)
 Check that the given string matches the user's date format.

Public Attributes

 $rules = array()
 $func_name = ""

Detailed Description

Definition at line 17 of file Validation.php.


Member Function Documentation

Validation::AddRule ( fieldname,
error_message,
function_name 
)

Adds a validation rule for a specific field upon submission of the form.

You must call RenderRules below RenderFields when outputing the page

Parameters:
string$fieldnameThe name of the field.
string$error_messageThe message to display on unsuccessful validation.
string$function_nameThe function to call to validate the field

Definition at line 53 of file Validation.php.

  {
    $this->rules[] = array($fieldname, $error_message, $function_name );
  }
Validation::not_empty ( field_string)

Checks if a string is empty.

Parameters:
string$field_stringThe field value that is being checked.
Returns:
boolean True if the string is not empty.

Definition at line 129 of file Validation.php.

  {
    return ($field_string != "");
  }
Validation::positive_dollars ( field_string)

Check that the given string is a positive dollar amount.

Use not_empty first if string is required.

Parameters:
string$field_stringThe amount to be checked.
Returns:
boolean Returns true if the given string is a positive dollar amount.

Definition at line 150 of file Validation.php.

  {
   if(!$field_string) return true;
   if( preg_match('/^\$?[0-9]*\.?[0-9]?[0-9]?$/', $field_string) ) {
     $field_string = preg_replace("/\$/", "", $field_string);
     $field_string = preg_replace("/\./", "", $field_string);
     if( intval($field_string) > 0 ) return true;
   }
   return false;
  }
Validation::positive_integer ( field_string)

Check that the given string is a positive integer.

Use not_empty first if string is required.

Parameters:
string$field_stringThe amount to be checked.
Returns:
boolean Returns true if the given string is a positive integer.

Definition at line 167 of file Validation.php.

  {
   if(!$field_string) return true;
    return ( preg_match('/^[0-9]*$/', $field_string) );
  }
Validation::RenderJavascript ( prefix = "")

Returns the javascript for form validation using the rules.

Parameters:
string$onsubmitThe name of the function called on submission of the form.
string$prefixOptional prefix for form fields.
Returns:
string HTML/Javascript for form validation.

Definition at line 64 of file Validation.php.

  {
    if(! count($this->rules) ) return "";

    $html = <<<EOHTML
<script language="JavaScript">
function $this->func_name(form)
{
  var error_message = "";\n
EOHTML;

    foreach($this->rules as $rule) {
      list($fieldname, $error_message, $function_name) = $rule;

    $html .= <<<EOHTML
if(!$function_name(form.$prefix$fieldname)) error_message += "$error_message\\n";
EOHTML;
    }

    $html .= <<<EOHTML
if(error_message == "") return true;
alert("Errors:"+"\\n"+error_message);
return false;
}
</script>
EOHTML;

    return $html;
  }
Validation::selected ( field_string)

Checks that a string is not empty or zero.

Parameters:
string$select_stringThe select value that is being checked.
Returns:
boolean True if the string is not empty or equal to 0.

Definition at line 139 of file Validation.php.

  {
    return (!($field_string == "" || $field_string == "0"));
  }
Validation::valid_date_format ( field_string)

Check that the given string matches the user's date format.

Use not_empty first if string is required.

Parameters:
string$field_stringThe string to be checked.
Returns:
boolean Returns true if the given string matches the user's date format from session.

Definition at line 193 of file Validation.php.

  {
   global $session;

   if(!$field_string) return true;

   switch($session->date_format_type) {
      case 'J':
        if (!preg_match('/^([0-9]{4})[\/\-]([0-9]{1,2})[\/\-]([0-9]{1,2})$/', $field_string, $regs)) return false;
        $day = intval($regs[3]);
        $month = intval($regs[2]);
        $year = intval($regs[1]);
        break;

      case 'U':
        if (!preg_match('/^([0-9]{1,2})[\/\-]([0-9]{1,2})[\/\-]([0-9]{4})$/', $field_string, $regs)) return false;
        $day = intval($regs[2]);
        $month = intval($regs[1]);
        $year = intval($regs[3]);
        break;

      case 'E':
      default:
        if (!preg_match('/^([0-9]{1,2})[\/\-]([0-9]{1,2})[\/\-]([0-9]{4})$/', $field_string, $regs)) return false;
        $day = intval($regs[1]);
        $month = intval($regs[2]);
        $year = intval($regs[3]);
   }
   return (checkdate ($month, $day, $year));
  }
Validation::valid_email_format ( field_string)

Check that the given string is a valid email address.

Use not_empty first if string is required.

Parameters:
string$field_stringThe string to be checked.
Returns:
boolean Returns true if the given string is a valid email address.

Definition at line 179 of file Validation.php.

  {
   if(!$field_string) return true;
   // Anything printable, followed by between 1 & 5 valid domain components, with a TLD to finish
   $pattern = "/^[[:print:]]+@([a-z0-9][a-z0-9-]*\.){1,5}[a-z]{2,5}$/i";
   return (preg_match($pattern, $field_string));
  }
Validation::Validate ( object)

Validates the form according to it's rules.

Parameters:
object$objectThe data object that requires form validation.
Returns:
boolean True if the validation succeeded.

Definition at line 99 of file Validation.php.

  {
    global $c;

    if(! count($this->rules) ) return;

    $valid = true;

    foreach($this->rules as $rule) {
      list($fieldname, $error_message, $function_name) = $rule;

      if (!$this->$function_name($object->Get($fieldname))) {
        $valid = false;
        $c->messages[] = $error_message;
      }

    }

    return $valid;
  }
Validation::Validation ( func_name)

@-

Initialise a new validation.

Parameters:
string$func_nameThe javascript function name to call onsubmit of the form

Definition at line 40 of file Validation.php.

  {
    $this->func_name = $func_name;
  }

Member Data Documentation

Definition at line 32 of file Validation.php.

Validation::$rules = array()

Definition at line 26 of file Validation.php.


The documentation for this class was generated from the following file: