FormError.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Form;
  11. use Symfony\Component\Form\Exception\BadMethodCallException;
  12. /**
  13. * Wraps errors in forms.
  14. *
  15. * @author Bernhard Schussek <bschussek@gmail.com>
  16. */
  17. class FormError
  18. {
  19. protected $messageTemplate;
  20. protected $messageParameters;
  21. protected $messagePluralization;
  22. private $message;
  23. private $cause;
  24. /**
  25. * The form that spawned this error.
  26. *
  27. * @var FormInterface
  28. */
  29. private $origin;
  30. /**
  31. * Any array key in $messageParameters will be used as a placeholder in
  32. * $messageTemplate.
  33. *
  34. * @param string $message The translated error message
  35. * @param string|null $messageTemplate The template for the error message
  36. * @param array $messageParameters The parameters that should be
  37. * substituted in the message template
  38. * @param int|null $messagePluralization The value for error message pluralization
  39. * @param mixed $cause The cause of the error
  40. *
  41. * @see \Symfony\Component\Translation\Translator
  42. */
  43. public function __construct(string $message, string $messageTemplate = null, array $messageParameters = [], int $messagePluralization = null, $cause = null)
  44. {
  45. $this->message = $message;
  46. $this->messageTemplate = $messageTemplate ?: $message;
  47. $this->messageParameters = $messageParameters;
  48. $this->messagePluralization = $messagePluralization;
  49. $this->cause = $cause;
  50. }
  51. /**
  52. * Returns the error message.
  53. *
  54. * @return string
  55. */
  56. public function getMessage()
  57. {
  58. return $this->message;
  59. }
  60. /**
  61. * Returns the error message template.
  62. *
  63. * @return string
  64. */
  65. public function getMessageTemplate()
  66. {
  67. return $this->messageTemplate;
  68. }
  69. /**
  70. * Returns the parameters to be inserted in the message template.
  71. *
  72. * @return array
  73. */
  74. public function getMessageParameters()
  75. {
  76. return $this->messageParameters;
  77. }
  78. /**
  79. * Returns the value for error message pluralization.
  80. *
  81. * @return int|null
  82. */
  83. public function getMessagePluralization()
  84. {
  85. return $this->messagePluralization;
  86. }
  87. /**
  88. * Returns the cause of this error.
  89. *
  90. * @return mixed The cause of this error
  91. */
  92. public function getCause()
  93. {
  94. return $this->cause;
  95. }
  96. /**
  97. * Sets the form that caused this error.
  98. *
  99. * This method must only be called once.
  100. *
  101. * @throws BadMethodCallException If the method is called more than once
  102. */
  103. public function setOrigin(FormInterface $origin)
  104. {
  105. if (null !== $this->origin) {
  106. throw new BadMethodCallException('setOrigin() must only be called once.');
  107. }
  108. $this->origin = $origin;
  109. }
  110. /**
  111. * Returns the form that caused this error.
  112. *
  113. * @return FormInterface|null The form that caused this error
  114. */
  115. public function getOrigin()
  116. {
  117. return $this->origin;
  118. }
  119. }