[iCalendar,vEvent,vComponent] Fix the line wrapping in V* components.
[awl.git] / inc / Validation.php
blob6587260451c6454ca7dd1aa7846b2923e3520ca3
1 <?php
2 /**
3 * Classes to handle validation of form data.
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
11 require_once("AWLUtilities.php");
13 /**
14 * Rules used for validation of form fields.
15 * @package awl
17 class Validation
19 /**#@+
20 * @access private
22 /**
23 * List of rules for validation
24 * @var rules
26 var $rules = array();
28 /**
29 * The javascript function name to call onsubmit of the form
30 * @var func_name
32 var $func_name = "";
34 /**#@-*/
36 /**
37 * Initialise a new validation.
38 * @param string $func_name The javascript function name to call onsubmit of the form
40 function Validation($func_name)
42 $this->func_name = $func_name;
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
53 function AddRule( $fieldname, $error_message, $function_name )
55 $this->rules[] = array($fieldname, $error_message, $function_name );
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.
64 function RenderJavascript($prefix = "")
66 if(! count($this->rules) ) return "";
68 $html = <<<EOHTML
69 <script language="JavaScript">
70 function $this->func_name(form)
72 var error_message = "";\n
73 EOHTML;
75 foreach($this->rules as $rule) {
76 list($fieldname, $error_message, $function_name) = $rule;
78 $html .= <<<EOHTML
79 if(!$function_name(form.$prefix$fieldname)) error_message += "$error_message\\n";
80 EOHTML;
83 $html .= <<<EOHTML
84 if(error_message == "") return true;
85 alert("Errors:"+"\\n"+error_message);
86 return false;
88 </script>
89 EOHTML;
91 return $html;
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.
99 function Validate($object)
101 global $c;
103 if(! count($this->rules) ) return;
105 $valid = true;
107 foreach($this->rules as $rule) {
108 list($fieldname, $error_message, $function_name) = $rule;
110 if (!$this->$function_name($object->Get($fieldname))) {
111 $valid = false;
112 $c->messages[] = $error_message;
117 return $valid;
120 ///////////////////////////
121 // VALIDATION FUNCTIONS
122 ///////////////////////////
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.
129 function not_empty($field_string)
131 return ($field_string != "");
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.
139 function selected($field_string)
141 return (!($field_string == "" || $field_string == "0"));
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.
150 function positive_dollars($field_string)
152 if(!$field_string) return true;
153 $pattern = "^\$?[0-9]*\.?[0-9]?[0-9]?$";
154 if( ereg($pattern, $field_string) ) {
155 $field_string = ereg_replace("\$", "", $field_string);
156 $field_string = ereg_replace("\.", "", $field_string);
157 if( intval($field_string) > 0 ) return true;
159 return false;
163 * Check that the given string is a positive integer.
164 * Use not_empty first if string is required.
165 * @param string $field_string The amount to be checked.
166 * @return boolean Returns true if the given string is a positive integer.
168 function positive_integer($field_string)
170 if(!$field_string) return true;
171 $pattern = "^[0-9]*$";
172 return ( ereg($pattern, $field_string) );
176 * Check that the given string is a valid email address.
177 * Use not_empty first if string is required.
178 * @param string $field_string The string to be checked.
179 * @return boolean Returns true if the given string is a valid email address.
181 function valid_email_format($field_string)
183 if(!$field_string) return true;
184 // Anything printable, followed by between 1 & 5 valid domain components, with a TLD to finish
185 $pattern = "/^[[:print:]]+@([a-z0-9][a-z0-9-]*\.){1,5}[a-z]{2,5}$/i";
186 return (preg_match($pattern, $field_string));
190 * Check that the given string matches the user's date format.
191 * Use not_empty first if string is required.
192 * @param string $field_string The string to be checked.
193 * @return boolean Returns true if the given string matches the user's date format from session.
195 function valid_date_format($field_string)
197 global $session;
199 if(!$field_string) return true;
201 switch($session->date_format_type) {
202 case 'J':
203 if (!ereg ("^([0-9]{4})[\/\-]([0-9]{1,2})[\/\-]([0-9]{1,2})$", $field_string, $regs)) return false;
204 $day = intval($regs[3]);
205 $month = intval($regs[2]);
206 $year = intval($regs[1]);
207 break;
209 case 'U':
210 if (!ereg ("^([0-9]{1,2})[\/\-]([0-9]{1,2})[\/\-]([0-9]{4})$", $field_string, $regs)) return false;
211 $day = intval($regs[2]);
212 $month = intval($regs[1]);
213 $year = intval($regs[3]);
214 break;
216 case 'E':
217 default:
218 if (!ereg ("^([0-9]{1,2})[\/\-]([0-9]{1,2})[\/\-]([0-9]{4})$", $field_string, $regs)) return false;
219 $day = intval($regs[1]);
220 $month = intval($regs[2]);
221 $year = intval($regs[3]);
223 return (checkdate ($month, $day, $year));