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.

217 lines
5.0 KiB

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