@@ -126,10 +126,17 @@ public function testValidateWithStringRulesNotFound(): void
126
126
public function testValidateWithStringRulesFoundReadMessagesFromValidationConfig (): void
127
127
{
128
128
$ validation = new class () extends ValidationConfig {
129
- public $ signup = [
129
+ /**
130
+ * @var array<string, string>
131
+ */
132
+ public array $ signup = [
130
133
'username ' => 'required ' ,
131
134
];
132
- public $ signup_errors = [
135
+
136
+ /**
137
+ * @var array<string, array<string, string>>
138
+ */
139
+ public array $ signup_errors = [
133
140
'username ' => [
134
141
'required ' => 'You must choose a username. ' ,
135
142
],
@@ -149,7 +156,10 @@ public function testValidateWithStringRulesFoundReadMessagesFromValidationConfig
149
156
public function testValidateWithStringRulesFoundUseMessagesParameter (): void
150
157
{
151
158
$ validation = new class () extends ValidationConfig {
152
- public $ signup = [
159
+ /**
160
+ * @var array<string, string>
161
+ */
162
+ public array $ signup = [
153
163
'username ' => 'required ' ,
154
164
];
155
165
};
@@ -191,6 +201,77 @@ public function testValidateData(): void
191
201
);
192
202
}
193
203
204
+ public function testValidateDataWithCustomErrorMessage (): void
205
+ {
206
+ // make sure we can instantiate one
207
+ $ this ->controller = new Controller ();
208
+ $ this ->controller ->initController ($ this ->request , $ this ->response , $ this ->logger );
209
+
210
+ $ method = $ this ->getPrivateMethodInvoker ($ this ->controller , 'validateData ' );
211
+
212
+ $ data = [
213
+ 'username ' => 'a ' ,
214
+ 'password ' => '123 ' ,
215
+ ];
216
+ $ rules = [
217
+ 'username ' => 'required|min_length[3] ' ,
218
+ 'password ' => 'required|min_length[10] ' ,
219
+ ];
220
+ $ errors = [
221
+ 'username ' => [
222
+ 'required ' => 'Please fill "{field}". ' ,
223
+ 'min_length ' => '"{field}" must be {param} letters or longer. ' ,
224
+ ],
225
+ ];
226
+ $ this ->assertFalse ($ method ($ data , $ rules , $ errors ));
227
+ $ this ->assertSame (
228
+ '"username" must be 3 letters or longer. ' ,
229
+ Services::validation ()->getError ('username ' )
230
+ );
231
+ $ this ->assertSame (
232
+ 'The password field must be at least 10 characters in length. ' ,
233
+ Services::validation ()->getError ('password ' )
234
+ );
235
+ }
236
+
237
+ public function testValidateDataWithCustomErrorMessageLabeledStyle (): void
238
+ {
239
+ // make sure we can instantiate one
240
+ $ this ->controller = new Controller ();
241
+ $ this ->controller ->initController ($ this ->request , $ this ->response , $ this ->logger );
242
+
243
+ $ method = $ this ->getPrivateMethodInvoker ($ this ->controller , 'validateData ' );
244
+
245
+ $ data = [
246
+ 'username ' => 'a ' ,
247
+ 'password ' => '123 ' ,
248
+ ];
249
+ $ rules = [
250
+ 'username ' => [
251
+ 'label ' => 'Username ' ,
252
+ 'rules ' => 'required|min_length[3] ' ,
253
+ 'errors ' => [
254
+ 'required ' => 'Please fill "{field}". ' ,
255
+ 'min_length ' => '"{field}" must be {param} letters or longer. ' ,
256
+ ],
257
+ ],
258
+ 'password ' => [
259
+ 'required|min_length[10] ' ,
260
+ 'label ' => 'Password ' ,
261
+ 'rules ' => 'required|min_length[10] ' ,
262
+ ],
263
+ ];
264
+ $ this ->assertFalse ($ method ($ data , $ rules ));
265
+ $ this ->assertSame (
266
+ '"Username" must be 3 letters or longer. ' ,
267
+ Services::validation ()->getError ('username ' )
268
+ );
269
+ $ this ->assertSame (
270
+ 'The Password field must be at least 10 characters in length. ' ,
271
+ Services::validation ()->getError ('password ' )
272
+ );
273
+ }
274
+
194
275
public function testHelpers (): void
195
276
{
196
277
$ this ->controller = new class () extends Controller {
0 commit comments