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.

154 lines
3.3 KiB

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-24
  8. */
  9. abstract class Form
  10. {
  11. const SUCCESS = 'success';
  12. const ERROR = 'error';
  13. /**
  14. * @var FormField[]
  15. */
  16. protected $fields = array();
  17. /**
  18. * @var array
  19. */
  20. protected $messages = array(
  21. self::SUCCESS => 'Form data valid',
  22. self::ERROR => 'Form data invalid');
  23. protected $valid = true;
  24. public function __construct()
  25. {
  26. $this->init();
  27. }
  28. /**
  29. * @param string $name
  30. * @param bool|string $message
  31. * @return FormField
  32. */
  33. protected function addField($name, $message = false)
  34. {
  35. $this->fields[$name] = new FormField($message);
  36. return $this->fields[$name];
  37. }
  38. public function isValid($data)
  39. {
  40. if (!is_array($data)) {
  41. throw new \Majestic\Exception\InitializationException(__CLASS__ . '::' . __METHOD__ . ' expects an array');
  42. }
  43. foreach ($this->fields as $field_name => $field) {
  44. if (isset($data[$field_name])) {
  45. $this->valid &= $field->isValid($data[$field_name], $data);
  46. } else {
  47. $this->valid &= $field->isValid(null, $data);
  48. }
  49. }
  50. if (!$this->valid) {
  51. $this->fillHelperData();
  52. }
  53. return $this->valid;
  54. }
  55. public function getMessages()
  56. {
  57. $messages = array();
  58. foreach ($this->fields as $name => $field) {
  59. if ($mess = $field->getMessage()) {
  60. $messages[$name] = $mess;
  61. }
  62. }
  63. return $messages;
  64. }
  65. public function getValue($key)
  66. {
  67. if (isset($this->fields[$key])) {
  68. return $this->fields[$key]->getValue();
  69. }
  70. return false;
  71. }
  72. /**
  73. * @return array
  74. */
  75. public function getValues()
  76. {
  77. $values = array();
  78. foreach ($this->fields as $key => $field) {
  79. if (!$field->isIgnored()) {
  80. $values[$key] = $field->getValue();
  81. }
  82. }
  83. return $values;
  84. }
  85. /**
  86. * @return array
  87. */
  88. public function getSourceValues()
  89. {
  90. $values = array();
  91. foreach ($this->fields as $key => $field) {
  92. $values[$key] = $field->getSourceValue();
  93. }
  94. return $values;
  95. }
  96. /**
  97. * @return string
  98. */
  99. public function getMessageType()
  100. {
  101. return ($this->valid) ? self::SUCCESS : self::ERROR;
  102. }
  103. /**
  104. * @return string
  105. */
  106. public function getMessage()
  107. {
  108. return $this->messages[$this->getMessageType()];
  109. }
  110. /**
  111. * @param string $message
  112. * @return Form
  113. */
  114. public function setSuccessMessage($message)
  115. {
  116. $this->messages[self::SUCCESS] = (string) $message;
  117. return $this;
  118. }
  119. /**
  120. * @param string $message
  121. * @return Form
  122. */
  123. public function setErrorMessage($message)
  124. {
  125. $this->messages[self::ERROR] = (string) $message;
  126. return $this;
  127. }
  128. protected function fillHelperData()
  129. {
  130. $data['messages'] = $this->getMessages();
  131. $data['values'] = $this->getSourceValues();
  132. \Majestic\Session\Session::set(get_class($this), $data);
  133. }
  134. abstract protected function init();
  135. }