Changeset 1443 for trunk/modules/forge/libraries/Form_Input.php
- Timestamp:
- 12/07/2007 12:50:49 PM (13 months ago)
- Files:
-
- 1 modified
-
trunk/modules/forge/libraries/Form_Input.php (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/modules/forge/libraries/Form_Input.php
r1442 r1443 17 17 protected $protect = array(); 18 18 19 // Valid tion rulesand callbacks19 // Validation rules, matches, and callbacks 20 20 protected $rules = array(); 21 protected $matches = array(); 21 22 protected $callbacks = array(); 22 23 … … 81 82 public function matches($input) 82 83 { 83 if ($this->validate() AND $input->validate() AND $this->value != $input->value) 84 { 85 $this->is_valid = FALSE; 86 87 $this->errors['matches'] = array($input->name, $this->name); 88 } 89 90 return $this->is_valid; 84 if ( ! in_array($input, $this->matches)) 85 { 86 $this->matches[] = $input; 87 } 88 89 return $this; 90 } 91 92 public function callback($callback) 93 { 94 if ( ! in_array($callback, $this->callbacks)) 95 { 96 $this->callbacks[] = $callback; 97 } 98 99 return $this; 91 100 } 92 101 … … 99 108 else 100 109 { 101 $this->label = ($val === TRUE) ? uc words(str_replace('_', ' ', $this->name)) : $val;110 $this->label = ($val === TRUE) ? ucfirst($this->name) : $val; 102 111 return $this; 103 112 } … … 157 166 } 158 167 168 public function add_error($key, $val) 169 { 170 if ( ! isset($this->errors[$key])) 171 { 172 $this->errors[$key] = $val; 173 } 174 175 return $this; 176 } 177 159 178 protected function error_message() 160 179 { … … 165 184 foreach($this->errors as $func => $args) 166 185 { 167 if ( ! is_array($args)) 168 { 169 $args = array(); 170 } 171 172 array_unshift($args, isset($this->data['label']) 173 ? strtolower($this->data['label']) 174 : $this->data['name']); 186 if (is_string($args)) 187 { 188 $error = $args; 189 } 190 else 191 { 192 // Force args to be an array 193 $args = is_array($args) ? $args : array(); 194 195 array_unshift($args, isset($this->data['label']) 196 ? strtolower($this->data['label']) 197 : $this->data['name']); 198 199 // Fetch an i18n error message 200 $error = Kohana::lang('validation.'.$func, $args); 201 } 175 202 176 203 // Make the error into HTML 177 $message .= '<p class="error">'.Kohana::lang('validation.'.$func, $args).'</p>'; 178 } 204 $message .= '<p class="error">'.$error.'</p>'; 205 } 206 179 207 return $message; 180 208 } … … 205 233 206 234 // No rules to validate 207 if (count($this->rules) == 0 )235 if (count($this->rules) == 0 AND count($this->matches) == 0 AND count($this->callbacks) == 0) 208 236 return $this->is_valid = TRUE; 209 237 … … 271 299 } 272 300 301 if ( ! empty($this->matches)) 302 { 303 foreach($this->matches as $input) 304 { 305 if ($this->value != $input->value) 306 { 307 // Field does not match 308 $this->errors['matches'] = array($input->name); 309 break; 310 } 311 } 312 } 313 314 if ( ! empty($this->callbacks)) 315 { 316 foreach($this->callbacks as $callback) 317 { 318 call_user_func($callback, $this); 319 320 // Stop when an error occurs 321 if ( ! empty($this->errors)) 322 break; 323 } 324 } 325 273 326 // If there are errors, validation failed 274 327 return $this->is_valid = empty($this->errors);
