Back to index

awl  0.53
Validation.php
Go to the documentation of this file.
00001 <?php
00011 require_once("AWLUtilities.php");
00012 
00017 class Validation
00018 {
00026   var $rules = array();
00027 
00032   var $func_name = "";
00033 
00040   function Validation($func_name)
00041   {
00042     $this->func_name = $func_name;
00043   }
00044 
00045 
00053   function AddRule( $fieldname, $error_message, $function_name )
00054   {
00055     $this->rules[] = array($fieldname, $error_message, $function_name );
00056   }
00057 
00064   function RenderJavascript($prefix = "")
00065   {
00066     if(! count($this->rules) ) return "";
00067 
00068     $html = <<<EOHTML
00069 <script language="JavaScript">
00070 function $this->func_name(form)
00071 {
00072   var error_message = "";\n
00073 EOHTML;
00074 
00075     foreach($this->rules as $rule) {
00076       list($fieldname, $error_message, $function_name) = $rule;
00077 
00078     $html .= <<<EOHTML
00079 if(!$function_name(form.$prefix$fieldname)) error_message += "$error_message\\n";
00080 EOHTML;
00081     }
00082 
00083     $html .= <<<EOHTML
00084 if(error_message == "") return true;
00085 alert("Errors:"+"\\n"+error_message);
00086 return false;
00087 }
00088 </script>
00089 EOHTML;
00090 
00091     return $html;
00092   }
00093 
00099   function Validate($object)
00100   {
00101     global $c;
00102 
00103     if(! count($this->rules) ) return;
00104 
00105     $valid = true;
00106 
00107     foreach($this->rules as $rule) {
00108       list($fieldname, $error_message, $function_name) = $rule;
00109 
00110       if (!$this->$function_name($object->Get($fieldname))) {
00111         $valid = false;
00112         $c->messages[] = $error_message;
00113       }
00114 
00115     }
00116 
00117     return $valid;
00118   }
00119 
00121 // VALIDATION FUNCTIONS
00123 
00129   function not_empty($field_string)
00130   {
00131     return ($field_string != "");
00132   }
00133 
00139   function selected($field_string)
00140   {
00141     return (!($field_string == "" || $field_string == "0"));
00142   }
00143 
00150   function positive_dollars($field_string)
00151   {
00152    if(!$field_string) return true;
00153    if( preg_match('/^\$?[0-9]*\.?[0-9]?[0-9]?$/', $field_string) ) {
00154      $field_string = preg_replace("/\$/", "", $field_string);
00155      $field_string = preg_replace("/\./", "", $field_string);
00156      if( intval($field_string) > 0 ) return true;
00157    }
00158    return false;
00159   }
00160 
00167   function positive_integer($field_string)
00168   {
00169    if(!$field_string) return true;
00170     return ( preg_match('/^[0-9]*$/', $field_string) );
00171   }
00172 
00179   function valid_email_format($field_string)
00180   {
00181    if(!$field_string) return true;
00182    // Anything printable, followed by between 1 & 5 valid domain components, with a TLD to finish
00183    $pattern = "/^[[:print:]]+@([a-z0-9][a-z0-9-]*\.){1,5}[a-z]{2,5}$/i";
00184    return (preg_match($pattern, $field_string));
00185   }
00186 
00193   function valid_date_format($field_string)
00194   {
00195    global $session;
00196 
00197    if(!$field_string) return true;
00198 
00199    switch($session->date_format_type) {
00200       case 'J':
00201         if (!preg_match('/^([0-9]{4})[\/\-]([0-9]{1,2})[\/\-]([0-9]{1,2})$/', $field_string, $regs)) return false;
00202         $day = intval($regs[3]);
00203         $month = intval($regs[2]);
00204         $year = intval($regs[1]);
00205         break;
00206 
00207       case 'U':
00208         if (!preg_match('/^([0-9]{1,2})[\/\-]([0-9]{1,2})[\/\-]([0-9]{4})$/', $field_string, $regs)) return false;
00209         $day = intval($regs[2]);
00210         $month = intval($regs[1]);
00211         $year = intval($regs[3]);
00212         break;
00213 
00214       case 'E':
00215       default:
00216         if (!preg_match('/^([0-9]{1,2})[\/\-]([0-9]{1,2})[\/\-]([0-9]{4})$/', $field_string, $regs)) return false;
00217         $day = intval($regs[1]);
00218         $month = intval($regs[2]);
00219         $year = intval($regs[3]);
00220    }
00221    return (checkdate ($month, $day, $year));
00222   }
00223 }
00224