PageRenderTime 51ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/fuel/core/classes/validation/error.php

https://bitbucket.org/codeyash/bootstrap
PHP | 182 lines | 91 code | 21 blank | 70 comment | 8 complexity | b2ee6a555a4ef6fdae0241065e5467b4 MD5 | raw file
Possible License(s): MIT, Apache-2.0
  1. <?php
  2. /**
  3. * Part of the Fuel framework.
  4. *
  5. * @package Fuel
  6. * @version 1.5
  7. * @author Fuel Development Team
  8. * @license MIT License
  9. * @copyright 2010 - 2013 Fuel Development Team
  10. * @link http://fuelphp.com
  11. */
  12. namespace Fuel\Core;
  13. /**
  14. * Validation error
  15. *
  16. * Contains all the information about a validation error
  17. *
  18. * @package Fuel
  19. * @category Core
  20. */
  21. class Validation_Error extends \Exception
  22. {
  23. /**
  24. * Load validation Language file when errors are thrown
  25. */
  26. public static function _init()
  27. {
  28. \Lang::load('validation', true);
  29. }
  30. /**
  31. * @var Fieldset_Field the field that caused the error
  32. */
  33. public $field;
  34. /**
  35. * @var mixed value that failed to validate
  36. */
  37. public $value;
  38. /**
  39. * @var string validation rule string representation
  40. */
  41. public $rule;
  42. /**
  43. * @var array variables passed to rule other than the value
  44. */
  45. public $params = array();
  46. /**
  47. * Constructor
  48. *
  49. * @param array Fieldset_Field object
  50. * @param mixed value that failed to validate
  51. * @param array contains rule name as key and callback as value
  52. * @param array additional rule params
  53. */
  54. public function __construct(Fieldset_Field $field, $value, $callback, $params)
  55. {
  56. $this->field = $field;
  57. $this->value = $value;
  58. $this->params = $params;
  59. $this->rule = key($callback);
  60. }
  61. /**
  62. * Get Message
  63. *
  64. * Shows the error message which can be taken from loaded language file.
  65. *
  66. * @param string HTML to prefix error message
  67. * @param string HTML to postfix error message
  68. * @param string Message to use, or false to try and load it from Lang class
  69. * @return string
  70. */
  71. public function get_message($msg = false, $open = '', $close = '')
  72. {
  73. $open = empty($open) ? \Config::get('validation.open_single_error', '') : $open;
  74. $close = empty($close) ? \Config::get('validation.close_single_error', '') : $close;
  75. if ($msg === false and ! ($msg = $this->field->get_error_message($this->rule)))
  76. {
  77. if (is_null($msg))
  78. {
  79. $msg = $this->field->fieldset()->validation()->get_message($this->rule);
  80. }
  81. if ($msg === false)
  82. {
  83. $msg = \Lang::get('validation.'.$this->rule) ?: \Lang::get('validation.'.\Arr::get(explode(':', $this->rule), 0));
  84. }
  85. }
  86. if ($msg == false)
  87. {
  88. return $open.'Validation rule '.$this->rule.' failed for '.$this->field->label.$close;
  89. }
  90. // only parse when there's tags in the message
  91. return $open.(strpos($msg, ':') === false ? $msg : $this->_replace_tags($msg)).$close;
  92. }
  93. /**
  94. * Replace templating tags with values
  95. *
  96. * @param error message to parse
  97. * @return string
  98. */
  99. protected function _replace_tags($msg)
  100. {
  101. // prepare label & value
  102. $label = is_array($this->field->label) ? $this->field->label['label'] : $this->field->label;
  103. $value = is_array($this->value) ? implode(', ', $this->value) : $this->value;
  104. if (\Config::get('validation.quote_labels', false) and strpos($label, ' ') !== false)
  105. {
  106. // put the label in quotes if it contains spaces
  107. $label = '"'.$label.'"';
  108. }
  109. // setup find & replace arrays
  110. $find = array(':field', ':label', ':value', ':rule');
  111. $replace = array($this->field->name, $label, $value, $this->rule);
  112. // add the params to the find & replace arrays
  113. foreach($this->params as $key => $val)
  114. {
  115. // Convert array (as far as possible)
  116. if (is_array($val))
  117. {
  118. $result = '';
  119. foreach ($val as $v)
  120. {
  121. if (is_array($v))
  122. {
  123. $v = '(array)';
  124. }
  125. elseif (is_object($v))
  126. {
  127. $v = '(object)';
  128. }
  129. elseif (is_bool($v))
  130. {
  131. $v = $v ? 'true' : 'false';
  132. }
  133. $result .= empty($result) ? $v : (', '.$v);
  134. }
  135. $val = $result;
  136. }
  137. elseif (is_bool($val))
  138. {
  139. $val = $val ? 'true' : 'false';
  140. }
  141. // Convert object with __toString or just the classname
  142. elseif (is_object($val))
  143. {
  144. $val = method_exists($val, '__toString') ? (string) $val : get_class($val);
  145. }
  146. $find[] = ':param:'.($key + 1);
  147. $replace[] = $val;
  148. }
  149. // execute find & replace and return
  150. return str_replace($find, $replace, $msg);
  151. }
  152. /**
  153. * Generate the error message
  154. *
  155. * @return string
  156. */
  157. public function __toString()
  158. {
  159. return $this->get_message();
  160. }
  161. }