You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
4.2 KiB

  1. <?php
  2. /**
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage form
  7. * @since 2010-04-25
  8. * @version SVN: $Id$
  9. * @filesource $URL$
  10. */
  11. class FormField
  12. {
  13. protected $validators = array();
  14. protected $filters = array();
  15. /**
  16. * Used instead message of validator if defined.
  17. *
  18. * @var string
  19. */
  20. protected $default_message = false;
  21. protected $message = false;
  22. protected $value;
  23. /* Flags */
  24. protected $required = true;
  25. protected $ignored = false;
  26. public function __construct($default_message = false)
  27. {
  28. $this->default_message = $default_message;
  29. }
  30. public function setRequired($flag)
  31. {
  32. $this->required = (bool) $flag;
  33. return $this;
  34. }
  35. public function isRequired()
  36. {
  37. return $this->required;
  38. }
  39. public function setIgnored($flag)
  40. {
  41. $this->ignored = (bool) $flag;
  42. return $this;
  43. }
  44. public function isIgnored()
  45. {
  46. return $this->ignored;
  47. }
  48. public function addValidators($validators)
  49. {
  50. foreach ($validators as $validator) {
  51. $this->addValidator($validator);
  52. }
  53. return $this;
  54. }
  55. public function addValidator($validator)
  56. {
  57. if ($validator instanceof iValidator) {
  58. $name = get_class($validator);
  59. } elseif (is_string($validator)) {
  60. $name = $validator . 'Validator';
  61. $validator = new $name();
  62. } else {
  63. throw new Exception('Invalid validator provided to addValidator; must be string or iValidator');
  64. }
  65. $this->validators[$name] = $validator;
  66. return $this;
  67. }
  68. public function addFilters($filters)
  69. {
  70. foreach ($filters as $filter) {
  71. $this->addFilter($filter);
  72. }
  73. return $this;
  74. }
  75. public function addFilter($filter)
  76. {
  77. if ($filter instanceof iFilter) {
  78. $name = get_class($filter);
  79. } elseif (is_string($filter)) {
  80. $name = $filter . 'Filter';
  81. $filter = new $name();
  82. } else {
  83. throw new Exception('Invalid filter provided to addFilter; must be string or iFilter');
  84. }
  85. $this->filters[$name] = $filter;
  86. return $this;
  87. }
  88. public function getValue()
  89. {
  90. $value = $this->value;
  91. if (is_array($value)) {
  92. array_walk_recursive($value, array($this, 'filterValue'));
  93. } else {
  94. $this->filterValue($value, $value);
  95. }
  96. return $value;
  97. }
  98. /**
  99. * $value & $key for array_walk_recursive
  100. *
  101. * @param mixed $value
  102. * @param mixed $key
  103. */
  104. protected function filterValue(&$value, &$key)
  105. {
  106. foreach ($this->filters as $filter) {
  107. $value = $filter->filter($value);
  108. }
  109. }
  110. public function getSourceValue()
  111. {
  112. return $this->value;
  113. }
  114. public function isValid($value, $context = null)
  115. {
  116. $this->value = $value;
  117. // filtered value here
  118. $value = $this->getValue();
  119. $valid = true;
  120. if ((($value === '') || ($value === null)) && !$this->isRequired()) {
  121. return $valid;
  122. }
  123. foreach ($this->validators as $validator) {
  124. if (is_array($value)) {
  125. foreach ($value as $val) {
  126. if (!$validator->isValid($val, $context)) {
  127. $valid = false;
  128. if (!$this->default_message) {
  129. throw new Exception('Define default message for array fields');
  130. }
  131. $this->message = $this->default_message;
  132. }
  133. }
  134. if ($valid) {
  135. continue;
  136. }
  137. } elseif ($validator->isValid($value, $context)) {
  138. continue;
  139. }
  140. $valid = false;
  141. $this->message = ($this->default_message) ? $this->default_message : $validator->getMessage();
  142. break;
  143. }
  144. return $valid;
  145. }
  146. public function getMessage()
  147. {
  148. return $this->message;
  149. }
  150. }