Overview

Packages

  • awl
    • AuthPlugin
    • AwlDatabase
    • Browser
    • classEditor
    • DataEntry
    • DataUpdate
    • EMail
    • iCalendar
    • MenuSet
    • PgQuery
    • Session
    • Translation
    • User
    • Utilities
    • Validation
    • vCalendar
    • vComponent
    • XMLDocument
    • XMLElement
  • None
  • PHP

Classes

  • Validation
  • Overview
  • Package
  • Class
  • Tree
  • Deprecated
  • Todo
  1: <?php
  2: /**
  3: * Classes to handle validation of form data.
  4: *
  5: * @package   awl
  6: * @subpackage   Validation
  7: * @author    Emily Mossman <emily@mcmillan.net.nz>
  8: * @copyright Catalyst IT Ltd
  9: * @license   http://gnu.org/copyleft/gpl.html GNU GPL v2 or later
 10: */
 11: require_once("AWLUtilities.php");
 12: 
 13: /**
 14: * Rules used for validation of form fields.
 15: * @package   awl
 16: */
 17: class Validation
 18: {
 19:   /**#@+
 20:   * @access private
 21:   */
 22:   /**
 23:   * List of rules for validation
 24:   * @var rules
 25:   */
 26:   var $rules = array();
 27: 
 28:   /**
 29:   * The javascript function name to call onsubmit of the form
 30:   * @var func_name
 31:   */
 32:   var $func_name = "";
 33: 
 34:   /**#@-*/
 35: 
 36:   /**
 37:   * Initialise a new validation.
 38:   * @param string $func_name The javascript function name to call onsubmit of the form
 39:   */
 40:   function Validation($func_name)
 41:   {
 42:     $this->func_name = $func_name;
 43:   }
 44: 
 45: 
 46:   /**
 47:   * Adds a validation rule for a specific field upon submission of the form.
 48:   * You must call RenderRules below RenderFields when outputing the page
 49:   * @param string $fieldname The name of the field.
 50:   * @param string $error_message The message to display on unsuccessful validation.
 51:   * @param string $function_name The function to call to validate the field
 52:   */
 53:   function AddRule( $fieldname, $error_message, $function_name )
 54:   {
 55:     $this->rules[] = array($fieldname, $error_message, $function_name );
 56:   }
 57: 
 58:   /**
 59:   * Returns the javascript for form validation using the rules.
 60:   * @param string $onsubmit The name of the function called on submission of the form.
 61:   * @param string $prefix Optional prefix for form fields.
 62:   * @return string HTML/Javascript for form validation.
 63:   */
 64:   function RenderJavascript($prefix = "")
 65:   {
 66:     if(! count($this->rules) ) return "";
 67: 
 68:     $html = <<<EOHTML
 69: <script language="JavaScript">
 70: function $this->func_name(form)
 71: {
 72:   var error_message = "";\n
 73: EOHTML;
 74: 
 75:     foreach($this->rules as $rule) {
 76:       list($fieldname, $error_message, $function_name) = $rule;
 77: 
 78:     $html .= <<<EOHTML
 79: if(!$function_name(form.$prefix$fieldname)) error_message += "$error_message\\n";
 80: EOHTML;
 81:     }
 82: 
 83:     $html .= <<<EOHTML
 84: if(error_message == "") return true;
 85: alert("Errors:"+"\\n"+error_message);
 86: return false;
 87: }
 88: </script>
 89: EOHTML;
 90: 
 91:     return $html;
 92:   }
 93: 
 94:   /**
 95:   * Validates the form according to it's rules.
 96:   * @param object $object The data object that requires form validation.
 97:   * @return boolean True if the validation succeeded.
 98:   */
 99:   function Validate($object)
100:   {
101:     global $c;
102: 
103:     if(! count($this->rules) ) return;
104: 
105:     $valid = true;
106: 
107:     foreach($this->rules as $rule) {
108:       list($fieldname, $error_message, $function_name) = $rule;
109: 
110:       if (!$this->$function_name($object->Get($fieldname))) {
111:         $valid = false;
112:         $c->messages[] = $error_message;
113:       }
114: 
115:     }
116: 
117:     return $valid;
118:   }
119: 
120: ///////////////////////////
121: // VALIDATION FUNCTIONS
122: ///////////////////////////
123: 
124:   /**
125:   * Checks if a string is empty
126:   * @param string $field_string The field value that is being checked.
127:   * @return boolean True if the string is not empty.
128:   */
129:   function not_empty($field_string)
130:   {
131:     return ($field_string != "");
132:   }
133: 
134:   /**
135:   * Checks that a string is not empty or zero
136:   * @param string $select_string The select value that is being checked.
137:   * @return boolean True if the string is not empty or equal to 0.
138:   */
139:   function selected($field_string)
140:   {
141:     return (!($field_string == "" || $field_string == "0"));
142:   }
143: 
144:   /**
145:   * Check that the given string is a positive dollar amount.
146:   * Use not_empty first if string is required.
147:   * @param string $field_string The amount to be checked.
148:   * @return boolean Returns true if the given string is a positive dollar amount.
149:   */
150:   function positive_dollars($field_string)
151:   {
152:    if(!$field_string) return true;
153:    if( preg_match('/^\$?[0-9]*\.?[0-9]?[0-9]?$/', $field_string) ) {
154:      $field_string = preg_replace("/\$/", "", $field_string);
155:      $field_string = preg_replace("/\./", "", $field_string);
156:      if( intval($field_string) > 0 ) return true;
157:    }
158:    return false;
159:   }
160: 
161:   /**
162:   * Check that the given string is a positive integer.
163:   * Use not_empty first if string is required.
164:   * @param string $field_string The amount to be checked.
165:   * @return boolean Returns true if the given string is a positive integer.
166:   */
167:   function positive_integer($field_string)
168:   {
169:    if(!$field_string) return true;
170:     return ( preg_match('/^[0-9]*$/', $field_string) );
171:   }
172: 
173:   /**
174:   * Check that the given string is a valid email address.
175:   * Use not_empty first if string is required.
176:   * @param string $field_string The string to be checked.
177:   * @return boolean Returns true if the given string is a valid email address.
178:   */
179:   function valid_email_format($field_string)
180:   {
181:    if(!$field_string) return true;
182:    // Anything printable, followed by between 1 & 5 valid domain components, with a TLD to finish
183:    $pattern = "/^[[:print:]]+@([a-z0-9][a-z0-9-]*\.){1,5}[a-z]{2,5}$/i";
184:    return (preg_match($pattern, $field_string));
185:   }
186: 
187:   /**
188:   * Check that the given string matches the user's date format.
189:   * Use not_empty first if string is required.
190:   * @param string $field_string The string to be checked.
191:   * @return boolean Returns true if the given string matches the user's date format from session.
192:   */
193:   function valid_date_format($field_string)
194:   {
195:    global $session;
196: 
197:    if(!$field_string) return true;
198: 
199:    switch($session->date_format_type) {
200:       case 'J':
201:         if (!preg_match('/^([0-9]{4})[\/\-]([0-9]{1,2})[\/\-]([0-9]{1,2})$/', $field_string, $regs)) return false;
202:         $day = intval($regs[3]);
203:         $month = intval($regs[2]);
204:         $year = intval($regs[1]);
205:         break;
206: 
207:       case 'U':
208:         if (!preg_match('/^([0-9]{1,2})[\/\-]([0-9]{1,2})[\/\-]([0-9]{4})$/', $field_string, $regs)) return false;
209:         $day = intval($regs[2]);
210:         $month = intval($regs[1]);
211:         $year = intval($regs[3]);
212:         break;
213: 
214:       case 'E':
215:       default:
216:         if (!preg_match('/^([0-9]{1,2})[\/\-]([0-9]{1,2})[\/\-]([0-9]{4})$/', $field_string, $regs)) return false;
217:         $day = intval($regs[1]);
218:         $month = intval($regs[2]);
219:         $year = intval($regs[3]);
220:    }
221:    return (checkdate ($month, $day, $year));
222:   }
223: }
224: 
225: 
AWL API documentation generated by ApiGen 2.8.0