PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/connvoi/dev
PHP | 161 lines | 70 code | 21 blank | 70 comment | 7 complexity | 04c585527730119ea701d889795cecd1 MD5 | raw file
Possible License(s): MIT, BSD-3-Clause
  1. <?php
  2. /**
  3. * Part of the Fuel framework.
  4. *
  5. * @package Fuel
  6. * @version 1.0
  7. * @author Fuel Development Team
  8. * @license MIT License
  9. * @copyright 2010 - 2012 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 to just a string "(array)", can't reliably implode as contents might be arrays/objects
  116. if (is_array($val))
  117. {
  118. $val = '(array)';
  119. }
  120. // Convert object with __toString or just the classname
  121. elseif (is_object($val))
  122. {
  123. $val = method_exists($val, '__toString') ? (string) $val : get_class($val);
  124. }
  125. $find[] = ':param:'.($key + 1);
  126. $replace[] = $val;
  127. }
  128. // execute find & replace and return
  129. return str_replace($find, $replace, $msg);
  130. }
  131. /**
  132. * Generate the error message
  133. *
  134. * @return string
  135. */
  136. public function __toString()
  137. {
  138. return $this->get_message();
  139. }
  140. }