11
11
12
12
namespace Symfony \Component \Form ;
13
13
14
+ use Symfony \Component \Form \Exception \BadMethodCallException ;
15
+
14
16
/**
15
17
* Wraps errors in forms
16
18
*
17
19
* @author Bernhard Schussek <bschussek@gmail.com>
18
20
*/
19
- class FormError
21
+ class FormError implements \Serializable
20
22
{
21
23
/**
22
24
* @var string
@@ -41,6 +43,18 @@ class FormError
41
43
*/
42
44
protected $ messagePluralization ;
43
45
46
+ /**
47
+ * The cause for this error
48
+ * @var mixed
49
+ */
50
+ private $ cause ;
51
+
52
+ /**
53
+ * The form that spawned this error
54
+ * @var FormInterface
55
+ */
56
+ private $ origin ;
57
+
44
58
/**
45
59
* Constructor
46
60
*
@@ -50,17 +64,19 @@ class FormError
50
64
* @param string $message The translated error message
51
65
* @param string|null $messageTemplate The template for the error message
52
66
* @param array $messageParameters The parameters that should be
53
- * substituted in the message template.
67
+ * substituted in the message template
54
68
* @param integer|null $messagePluralization The value for error message pluralization
69
+ * @param mixed $cause The cause of the error
55
70
*
56
71
* @see \Symfony\Component\Translation\Translator
57
72
*/
58
- public function __construct ($ message , $ messageTemplate = null , array $ messageParameters = array (), $ messagePluralization = null )
73
+ public function __construct ($ message , $ messageTemplate = null , array $ messageParameters = array (), $ messagePluralization = null , $ cause = null )
59
74
{
60
75
$ this ->message = $ message ;
61
76
$ this ->messageTemplate = $ messageTemplate ?: $ message ;
62
77
$ this ->messageParameters = $ messageParameters ;
63
78
$ this ->messagePluralization = $ messagePluralization ;
79
+ $ this ->cause = $ cause ;
64
80
}
65
81
66
82
/**
@@ -102,4 +118,68 @@ public function getMessagePluralization()
102
118
{
103
119
return $ this ->messagePluralization ;
104
120
}
121
+
122
+ /**
123
+ * Returns the cause of this error.
124
+ *
125
+ * @return mixed The cause of this error
126
+ */
127
+ public function getCause ()
128
+ {
129
+ return $ this ->cause ;
130
+ }
131
+
132
+ /**
133
+ * Sets the form that caused this error.
134
+ *
135
+ * This method must only be called once.
136
+ *
137
+ * @param FormInterface $origin The form that caused this error
138
+ *
139
+ * @throws BadMethodCallException If the method is called more than once
140
+ */
141
+ public function setOrigin (FormInterface $ origin )
142
+ {
143
+ if (null !== $ this ->origin ) {
144
+ throw new BadMethodCallException ('setOrigin() must only be called once. ' );
145
+ }
146
+
147
+ $ this ->origin = $ origin ;
148
+ }
149
+
150
+ /**
151
+ * Returns the form that caused this error.
152
+ *
153
+ * @return FormInterface The form that caused this error
154
+ */
155
+ public function getOrigin ()
156
+ {
157
+ return $ this ->origin ;
158
+ }
159
+
160
+ /**
161
+ * Serializes this error.
162
+ *
163
+ * @return string The serialized error
164
+ */
165
+ public function serialize ()
166
+ {
167
+ return serialize (array (
168
+ $ this ->message ,
169
+ $ this ->messageTemplate ,
170
+ $ this ->messageParameters ,
171
+ $ this ->messagePluralization ,
172
+ $ this ->cause
173
+ ));
174
+ }
175
+
176
+ /**
177
+ * Unserializes a serialized error.
178
+ *
179
+ * @param string $serialized The serialized error
180
+ */
181
+ public function unserialize ($ serialized )
182
+ {
183
+ list ($ this ->message , $ this ->messageTemplate , $ this ->messageParameters , $ this ->messagePluralization , $ this ->cause ) = unserialize ($ serialized );
184
+ }
105
185
}
0 commit comments