Mixin_Validation

The NextGEN Gallery Mixin Validation class.

Defined (1)

The class is defined in the following location(s).

/products/photocrati_nextgen/modules/validation/package.module.validation.php  
  1. class Mixin_Validation extends Mixin 
  2. var $_default_msgs = array("validates_presence_of" => "%s should be present", "validates_presence_with" => "%s should be present with %s", "validates_uniqueness_of" => "%s should be unique", "validates_confirmation_of" => "%s should match confirmation", "validates_exclusion_of" => "%s is reserved", "validates_format_of" => "%s is invalid", "validates_inclusion_of" => "%s is not included in the list", "validates_numericality_of" => "%s is not numeric", "validates_less_than" => "%s is too small", "validates_greater_than" => "%s is too large", "validates_equals" => "%s is invalid"); 
  3. var $_default_patterns = array('email_address' => "//"); 
  4. /** 
  5. * Clears all errors for the object 
  6. */ 
  7. function clear_errors() 
  8. $this->object->_errors = array(); 
  9. /** 
  10. * Returns the errors for a particular property 
  11. * @param string $property 
  12. * @return array|null 
  13. */ 
  14. function errors_for($property) 
  15. $errors = $this->object->_errors; 
  16. if (isset($errors[$property])) { 
  17. return $errors[$property]; 
  18. } else { 
  19. return NULL; 
  20. /** 
  21. * Adds an error for a particular property of the object 
  22. * @param string $property 
  23. * @param string $msg 
  24. */ 
  25. function add_error($msg, $property = '*') 
  26. if (!isset($this->object->_errors)) { 
  27. $this->object->_errors = array(); 
  28. $errors =& $this->object->_errors; 
  29. if (!isset($errors[$property])) { 
  30. $errors[$property] = array(); 
  31. $errors[$property][] = $msg; 
  32. /** 
  33. * Returns the default error message for a particular validator. 
  34. * A hook could override this, or this class could be subclassed 
  35. * @param string $validator 
  36. * @return string 
  37. */ 
  38. function _get_default_error_message_for($validator) 
  39. $retval = FALSE; 
  40. // The $validator variable is often set to __METHOD__, and many 
  41. // forget that __METHOD__ looks like this: 
  42. // Mixin_Active_Record_Validation::validates_presence_of 
  43. // So, we fix that 
  44. if (strpos($validator, '::') !== FALSE) { 
  45. $parts = explode('::', $validator); 
  46. $validator = $parts[1]; 
  47. // Ensure that the validator has a default error message 
  48. if (isset($this->_default_msgs[$validator])) { 
  49. $retval = $this->_default_msgs[$validator]; 
  50. return $retval; 
  51. /** 
  52. * Returns the default pattern for a formatter, such as an "e-mail address". 
  53. * @param string $formatter 
  54. * @return string 
  55. */ 
  56. function get_default_pattern_for($formatter) 
  57. $retval = FALSE; 
  58. if (isset($this->_default_patterns[$formatter])) { 
  59. $retval = $this->_default_patterns[$formatter]; 
  60. return $retval; 
  61. /** 
  62. * Gets all of the errors for the object 
  63. * @return type 
  64. */ 
  65. function get_errors($property = FALSE) 
  66. $retval = $property ? $this->object->errors_for($property) : $this->object->_errors; 
  67. if (!$retval || is_array($retval)) { 
  68. $retval = array(); 
  69. return $retval; 
  70. /** 
  71. * Determines if an object, or a particular field for that object, has 
  72. * errors 
  73. * @param string $property 
  74. * @return boolean 
  75. */ 
  76. function is_valid($property = FALSE) 
  77. $valid = TRUE; 
  78. $errors = $this->object->get_errors(); 
  79. if ($property) { 
  80. if (isset($errors[$property]) && !empty($errors[$property])) { 
  81. $valid = FALSE; 
  82. } elseif (!empty($errors)) { 
  83. $valid = FALSE; 
  84. return $valid; 
  85. /** 
  86. * Determines if the object, or a particular field on the object, has errors 
  87. * @param type $property 
  88. * @return type 
  89. */ 
  90. function is_invalid($property = FALSE) 
  91. return !$this->object->is_valid($property); 
  92. /** 
  93. * Calls the validation method for a record, clearing the previous errors 
  94. */ 
  95. function validate() 
  96. $this->clear_errors(); 
  97. if ($this->object->has_method('validation')) { 
  98. $this->object->validation(); 
  99. return $this->object->is_valid(); 
  100. /** 
  101. * Converts the name of a property to a human readable property name 
  102. * E.g. how_did_you_hear_about_us to "How did you hear about us" 
  103. * @param string $str 
  104. * @return string 
  105. */ 
  106. function humanize_string($str) 
  107. $retval = array(); 
  108. if (is_array($str)) { 
  109. foreach ($str as $s) { 
  110. $retval[] = $this->humanize_string($s); 
  111. } else { 
  112. $retval = ucfirst(str_replace('_', ' ', $str)); 
  113. return $retval; 
  114. /** 
  115. * Validates the length of a property's value 
  116. * @param type $property 
  117. * @param type $length 
  118. * @param type $msg 
  119. */ 
  120. function validates_length_of($property, $length, $comparison_operator = '=', $msg = FALSE) 
  121. $valid = TRUE; 
  122. $value = $this->object->{$property}; 
  123. $default_msg = $this->_get_default_error_message_for(__METHOD__); 
  124. if (!$this->is_empty($value)) { 
  125. switch ($comparison_operator) { 
  126. case '=': 
  127. case '==': 
  128. $valid = strlen($value) == $comparison; 
  129. $default_msg = $this->_get_default_error_message_for('validates_equals'); 
  130. break; 
  131. case '!=': 
  132. case '!': 
  133. $valid = strlen($value) != $comparison; 
  134. $default_msg = $this->_get_default_error_message_for('validates_equals'); 
  135. break; 
  136. case '<': 
  137. $valid = strlen($value) < $comparion; 
  138. $default_msg = $this->_get_default_error_message_for('validates_less_than'); 
  139. break; 
  140. case '>': 
  141. $valid = strlen($value) > $comparison; 
  142. $default_msg = $this->_get_default_error_message_for('validates_greater_than'); 
  143. break; 
  144. case '<=': 
  145. $valid = strlen($value) <= $comparison; 
  146. $default_msg = $this->_get_default_error_message_for('validates_less_than'); 
  147. break; 
  148. case '>=': 
  149. $valid = strlen($value) >= $comparion; 
  150. $default_msg = $this->_get_default_error_message_for('validates_greater_than'); 
  151. break; 
  152. } else { 
  153. $valid = FALSE; 
  154. if (!$valid) { 
  155. if (!$msg) { 
  156. $error_msg = sprintf($default_msg, $this->humanize_string($property)); 
  157. } else { 
  158. $error_msg = $msg; 
  159. $this->add_error($error_msg, $property); 
  160. /** 
  161. * Validates that a property contains a numeric value. May optionally be tested against 
  162. * other numbers. 
  163. * @param string $property 
  164. * @param decimal $comparison 
  165. * @param string $comparison_operator 
  166. * @param type $msg 
  167. */ 
  168. function validates_numericality_of($property, $comparison = FALSE, $comparison_operator = FALSE, $int_only = FALSE, $msg = FALSE) 
  169. $properties = is_array($property) ? $property : array($property); 
  170. foreach ($properties as $property) { 
  171. $value = $this->object->{$property}; 
  172. $default_msg = $this->_get_default_error_message_for(__METHOD__); 
  173. if (!$this->is_empty($value)) { 
  174. $invalid = FALSE; 
  175. if (is_numeric($value)) { 
  176. $value = $value += 0; 
  177. if ($int_only) { 
  178. $invalid = !is_int($value); 
  179. if (!$invalid) { 
  180. switch ($comparison_operator) { 
  181. case '=': 
  182. case '==': 
  183. $invalid = $value == $comparison ? FALSE : TRUE; 
  184. $default_msg = $this->_get_default_error_message_for('validates_equals'); 
  185. break; 
  186. case '!=': 
  187. case '!': 
  188. $invalid = $value != $comparison ? FALSE : TRUE; 
  189. $default_msg = $this->_get_default_error_message_for('validates_equals'); 
  190. break; 
  191. case '<': 
  192. $invalid = $value < $comparison ? FALSE : TRUE; 
  193. $default_msg = $this->_get_default_error_message_for('validates_less_than'); 
  194. break; 
  195. case '>': 
  196. $invalid = $value > $comparison ? FALSE : TRUE; 
  197. $default_msg = $this->_get_default_error_message_for('validates_greater_than'); 
  198. break; 
  199. case '<=': 
  200. $invalid = $value <= $comparison ? FALSE : TRUE; 
  201. $default_msg = $this->_get_default_error_message_for('validates_less_than'); 
  202. break; 
  203. case '>=': 
  204. $invalid = $value >= $comparison ? FALSE : TRUE; 
  205. $default_msg = $this->_get_default_error_message_for('validates_greater_than'); 
  206. break; 
  207. } else { 
  208. $invalid = TRUE; 
  209. if ($invalid) { 
  210. if (!$msg) { 
  211. $error_msg = sprintf($default_msg, $this->humanize_string($property)); 
  212. } else { 
  213. $error_msg = $msg; 
  214. $this->add_error($error_msg, $property); 
  215. /** 
  216. * Validates that a property includes a particular value 
  217. * @param string $property 
  218. * @param array $values 
  219. * @param string $msg 
  220. */ 
  221. function validates_inclusion_of($property, $values = array(), $msg = FALSE) 
  222. if (!is_array($values)) { 
  223. $values = array($values); 
  224. if (!in_array($this->object->{$property}, $values)) { 
  225. if (!$msg) { 
  226. $msg = $this->_get_default_error_message_for(__METHOD__); 
  227. $msg = sprintf($msg, $this->humanize_string($property)); 
  228. $this->add_error($msg, $property); 
  229. /** 
  230. * Validates that a property's value matches a particular pattern 
  231. * @param string|array $property 
  232. * @param string $pattern 
  233. * @param string $msg 
  234. */ 
  235. function validates_format_of($property, $pattern, $msg = FALSE) 
  236. if (!is_array($property)) { 
  237. $property = array($property); 
  238. // A pattern could be the name of a default pattern, or a regex pattern 
  239. $default_pattern = $this->get_default_pattern_for($pattern); 
  240. if ($default_pattern) { 
  241. $default_pattern = $pattern; 
  242. foreach ($property as $prop) { 
  243. // We do not validate blank values - we rely on "validates_presense_of" 
  244. // for that 
  245. if (!$this->is_empty($this->object->{$prop})) { 
  246. // If it doesn't match, then it's an error 
  247. if (!preg_match($pattern, $property)) { 
  248. // Get default message 
  249. if (!$msg) { 
  250. $msg = $this->_get_default_error_message_for(__METHOD__); 
  251. $msg = sprintf($msg, $this->humanize_string($property)); 
  252. $this->add_error($msg, $property); 
  253. /** 
  254. * Ensures that a property does NOT have a particular value 
  255. * @param string $property 
  256. * @param array $exclusions 
  257. * @param string $msg 
  258. */ 
  259. function validates_exclusion_of($property, $exclusions = array(), $msg = FALSE) 
  260. $invalid = FALSE; 
  261. if (!is_array($exclusions)) { 
  262. $exclusions = array($exclusions); 
  263. foreach ($exclusions as $exclusion) { 
  264. if ($exclusion == $this->object->{$property}) { 
  265. $invalid = TRUE; 
  266. break; 
  267. if ($invalid) { 
  268. if (!$msg) { 
  269. $msg = $this->_get_default_error_message_for(__METHOD__); 
  270. $msg = sprintf($msg, $this->humanize_string($property)); 
  271. $this->add_error($msg, $property); 
  272. /** 
  273. * Validates the confirmation of a property 
  274. * @param string $property 
  275. * @param string $confirmation 
  276. * @param string $msg 
  277. */ 
  278. function validates_confirmation_of($property, $confirmation, $msg = FALSE) 
  279. if ($this->object->{$property} != $this->object->{$confirmation}) { 
  280. if (!$msg) { 
  281. $msg = $this->_get_default_error_message_for(__METHOD__); 
  282. $msg = sprintf($msg, $this->humanize_string($property)); 
  283. $this->add_error($msg, $property); 
  284. /** 
  285. * Validates the uniqueness of a property 
  286. * @param string $property 
  287. * @param array $scope 
  288. * @param string $msg 
  289. */ 
  290. function validates_uniqueness_of($property, $scope = array(), $msg = FALSE) 
  291. // Get any entities that have the same property 
  292. $mapper = $this->object->get_mapper(); 
  293. $key = $mapper->get_primary_key_column(); 
  294. $mapper->select($key); 
  295. $mapper->limit(1); 
  296. $mapper->where_and(array("{$property} = %s", $this->object->{$property})); 
  297. if (!$this->object->is_new()) { 
  298. $mapper->where_and(array("{$key} != %s", $this->object->id())); 
  299. foreach ($scope as $another_property) { 
  300. $mapper->where_and(array("{$another_property} = %s", $another_property)); 
  301. $result = $mapper->run_query(); 
  302. // If there's a result, it means that the entity is NOT unique 
  303. if ($result) { 
  304. // Get default msg 
  305. if (!$msg) { 
  306. $msg = $this->_get_default_error_message_for(__METHOD__); 
  307. $msg = sprintf($msg, $this->humanize_string($property)); 
  308. // Add error 
  309. $this->add_error($msg, $property); 
  310. /** 
  311. * Validates the presence of a value for a particular field 
  312. * @param string $property 
  313. * @param string $msg 
  314. */ 
  315. function validates_presence_of($properties, $with = array(), $msg = FALSE) 
  316. $missing = array(); 
  317. if (!is_array($properties)) { 
  318. $properties = array($properties); 
  319. // Iterate through each property that we're to check, and ensure 
  320. // a value is present 
  321. foreach ($properties as $property) { 
  322. $invalid = TRUE; 
  323. // Is a value present? 
  324. if (!$this->is_empty($this->object->{$property})) { 
  325. $invalid = FALSE; 
  326. // This property must be present with at least another property 
  327. if ($with) { 
  328. if (!is_array($with)) { 
  329. $with = array($with); 
  330. foreach ($with as $other) { 
  331. if ($this->is_empty($this->object->{$other})) { 
  332. $invalid = TRUE; 
  333. $missing[] = $other; 
  334. // Add error 
  335. if ($invalid) { 
  336. if (!$msg) { 
  337. // If missing isn't empty, it means that we're to use the 
  338. // "with" error message 
  339. if ($missing) { 
  340. $missing = implode(', ', $this->humanize_string($missing)); 
  341. $msg = sprintf($this->_get_default_error_message_for('validates_presence_with'), $property, $missing); 
  342. } else { 
  343. $msg = sprintf($this->_get_default_error_message_for(__METHOD__), $property); 
  344. $this->add_error($msg, $property);