Skip to content

Commit 0db9279

Browse files
committed
Improve invalid messages for 'in', 'not_in', 'mimes', and 'uploaded_files' rules
1 parent bb8c375 commit 0db9279

File tree

5 files changed

+174
-7
lines changed

5 files changed

+174
-7
lines changed

src/Rules/In.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
namespace Rakit\Validation\Rules;
44

5+
use Rakit\Validation\Helper;
56
use Rakit\Validation\Rule;
67

78
class In extends Rule
89
{
910

1011
/** @var string */
11-
protected $message = "The :attribute is not allowing :value";
12+
protected $message = "The :attribute only allows :allowed_values";
1213

1314
/** @var bool */
1415
protected $strict = false;
@@ -49,7 +50,12 @@ public function check($value): bool
4950
{
5051
$this->requireParameters(['allowed_values']);
5152

52-
$allowed_values = $this->parameter('allowed_values');
53-
return in_array($value, $allowed_values, $this->strict);
53+
$allowedValues = $this->parameter('allowed_values');
54+
55+
$or = $this->validation ? $this->validation->getTranslation('or') : 'or';
56+
$allowedValuesText = Helper::join(Helper::wraps($allowedValues, "'"), ', ', ", {$or} ");
57+
$this->setParameterText('allowed_values', $allowedValuesText);
58+
59+
return in_array($value, $allowedValues, $this->strict);
5460
}
5561
}

src/Rules/Mimes.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22

33
namespace Rakit\Validation\Rules;
44

5-
use Rakit\Validation\Rule;
5+
use Rakit\Validation\Helper;
66
use Rakit\Validation\MimeTypeGuesser;
7+
use Rakit\Validation\Rule;
78

89
class Mimes extends Rule
910
{
1011
use Traits\FileTrait;
1112

1213
/** @var string */
13-
protected $message = "The :attribute file type is not allowed";
14+
protected $message = "The :attribute file type must be :allowed_types";
1415

1516
/** @var string|int */
1617
protected $maxSize = null;
@@ -60,6 +61,11 @@ public function check($value): bool
6061
{
6162
$allowedTypes = $this->parameter('allowed_types');
6263

64+
if ($allowedTypes) {
65+
$or = $this->validation ? $this->validation->getTranslation('or') : 'or';
66+
$this->setParameterText('allowed_types', Helper::join(Helper::wraps($allowedTypes, "'"), ', ', ", {$or} "));
67+
}
68+
6369
// below is Required rule job
6470
if (!$this->isValueFromUploadedFiles($value) or $value['error'] == UPLOAD_ERR_NO_FILE) {
6571
return true;

src/Rules/NotIn.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
namespace Rakit\Validation\Rules;
44

5+
use Rakit\Validation\Helper;
56
use Rakit\Validation\Rule;
67

78
class NotIn extends Rule
89
{
910

1011
/** @var string */
11-
protected $message = "The :attribute is not allowing :value";
12+
protected $message = "The :attribute is not allowing :disallowed_values";
1213

1314
/** @var bool */
1415
protected $strict = false;
@@ -48,7 +49,13 @@ public function strict($strict = true)
4849
public function check($value): bool
4950
{
5051
$this->requireParameters(['disallowed_values']);
52+
5153
$disallowedValues = (array) $this->parameter('disallowed_values');
54+
55+
$and = $this->validation ? $this->validation->getTranslation('and') : 'and';
56+
$disallowedValuesText = Helper::join(Helper::wraps($disallowedValues, "'"), ', ', ", {$and} ");
57+
$this->setParameterText('disallowed_values', $disallowedValuesText);
58+
5259
return !in_array($value, $disallowedValues, $this->strict);
5360
}
5461
}

src/Rules/UploadedFile.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class UploadedFile extends Rule implements BeforeValidate
1212
use Traits\FileTrait, Traits\SizeTrait;
1313

1414
/** @var string */
15-
protected $message = "The :attribute is not valid";
15+
protected $message = "The :attribute is not valid uploaded file";
1616

1717
/** @var string|int */
1818
protected $maxSize = null;
@@ -133,6 +133,11 @@ public function check($value): bool
133133
$maxSize = $this->parameter('max_size');
134134
$allowedTypes = $this->parameter('allowed_types');
135135

136+
if ($allowedTypes) {
137+
$or = $this->validation ? $this->validation->getTranslation('or') : 'or';
138+
$this->setParameterText('allowed_types', Helper::join(Helper::wraps($allowedTypes, "'"), ', ', ", {$or} "));
139+
}
140+
136141
// below is Required rule job
137142
if (!$this->isValueFromUploadedFiles($value) or $value['error'] == UPLOAD_ERR_NO_FILE) {
138143
return true;
@@ -150,13 +155,15 @@ public function check($value): bool
150155
if ($minSize) {
151156
$bytesMinSize = $this->getBytesSize($minSize);
152157
if ($value['size'] < $bytesMinSize) {
158+
$this->setMessage('The :attribute file is too small, minimum size is :min_size');
153159
return false;
154160
}
155161
}
156162

157163
if ($maxSize) {
158164
$bytesMaxSize = $this->getBytesSize($maxSize);
159165
if ($value['size'] > $bytesMaxSize) {
166+
$this->setMessage('The :attribute file is too large, maximum size is :max_size');
160167
return false;
161168
}
162169
}
@@ -167,6 +174,7 @@ public function check($value): bool
167174
unset($guesser);
168175

169176
if (!in_array($ext, $allowedTypes)) {
177+
$this->setMessage('The :attribute file type must be :allowed_types');
170178
return false;
171179
}
172180
}

tests/ValidatorTest.php

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,4 +1277,144 @@ public function testGetInvalidData()
12771277
$this->assertFalse(isset($stuffs['one']));
12781278
$this->assertFalse(isset($stuffs['two']));
12791279
}
1280+
1281+
public function testRuleInInvalidMessages()
1282+
{
1283+
$validation = $this->validator->validate([
1284+
'number' => 1
1285+
], [
1286+
'number' => 'in:7,8,9',
1287+
]);
1288+
1289+
$this->assertEquals($validation->errors()->first('number'), "The Number only allows '7', '8', or '9'");
1290+
1291+
// Using translation
1292+
$this->validator->setTranslation('or', 'atau');
1293+
1294+
$validation = $this->validator->validate([
1295+
'number' => 1
1296+
], [
1297+
'number' => 'in:7,8,9',
1298+
]);
1299+
1300+
$this->assertEquals($validation->errors()->first('number'), "The Number only allows '7', '8', atau '9'");
1301+
}
1302+
1303+
public function testRuleNotInInvalidMessages()
1304+
{
1305+
$validation = $this->validator->validate([
1306+
'number' => 1
1307+
], [
1308+
'number' => 'not_in:1,2,3',
1309+
]);
1310+
1311+
$this->assertEquals($validation->errors()->first('number'), "The Number is not allowing '1', '2', and '3'");
1312+
1313+
// Using translation
1314+
$this->validator->setTranslation('and', 'dan');
1315+
1316+
$validation = $this->validator->validate([
1317+
'number' => 1
1318+
], [
1319+
'number' => 'not_in:1,2,3',
1320+
]);
1321+
1322+
$this->assertEquals($validation->errors()->first('number'), "The Number is not allowing '1', '2', dan '3'");
1323+
}
1324+
1325+
public function testRuleMimesInvalidMessages()
1326+
{
1327+
$file = [
1328+
'name' => 'sample.txt',
1329+
'type' => 'plain/text',
1330+
'tmp_name' => __FILE__,
1331+
'size' => 1000,
1332+
'error' => UPLOAD_ERR_OK,
1333+
];
1334+
1335+
$validation = $this->validator->validate([
1336+
'sample' => $file,
1337+
], [
1338+
'sample' => 'mimes:jpeg,png,bmp',
1339+
]);
1340+
1341+
$expectedMessage = "The Sample file type must be 'jpeg', 'png', or 'bmp'";
1342+
$this->assertEquals($validation->errors()->first('sample'), $expectedMessage);
1343+
1344+
// Using translation
1345+
$this->validator->setTranslation('or', 'atau');
1346+
1347+
$validation = $this->validator->validate([
1348+
'sample' => $file,
1349+
], [
1350+
'sample' => 'mimes:jpeg,png,bmp',
1351+
]);
1352+
1353+
$expectedMessage = "The Sample file type must be 'jpeg', 'png', atau 'bmp'";
1354+
$this->assertEquals($validation->errors()->first('sample'), $expectedMessage);
1355+
}
1356+
1357+
public function testRuleUploadedFileInvalidMessages()
1358+
{
1359+
$file = [
1360+
'name' => 'sample.txt',
1361+
'type' => 'plain/text',
1362+
'tmp_name' => __FILE__,
1363+
'size' => 1024 * 1024 * 2, // 2M
1364+
'error' => UPLOAD_ERR_OK,
1365+
];
1366+
1367+
$rule = $this->getMockedUploadedFileRule();
1368+
1369+
// Invalid uploaded file (!is_uploaded_file($file['tmp_name']))
1370+
$validation = $this->validator->validate([
1371+
'sample' => $file,
1372+
], [
1373+
'sample' => 'uploaded_file',
1374+
]);
1375+
1376+
$expectedMessage = "The Sample is not valid uploaded file";
1377+
$this->assertEquals($validation->errors()->first('sample'), $expectedMessage);
1378+
1379+
// Invalid min size
1380+
$validation = $this->validator->validate([
1381+
'sample' => $file,
1382+
], [
1383+
'sample' => [(clone $rule)->minSize('3M')],
1384+
]);
1385+
1386+
$expectedMessage = "The Sample file is too small, minimum size is 3M";
1387+
$this->assertEquals($validation->errors()->first('sample'), $expectedMessage);
1388+
1389+
// Invalid max size
1390+
$validation = $this->validator->validate([
1391+
'sample' => $file,
1392+
], [
1393+
'sample' => [(clone $rule)->maxSize('1M')],
1394+
]);
1395+
1396+
$expectedMessage = "The Sample file is too large, maximum size is 1M";
1397+
$this->assertEquals($validation->errors()->first('sample'), $expectedMessage);
1398+
1399+
// Invalid file types
1400+
$validation = $this->validator->validate([
1401+
'sample' => $file,
1402+
], [
1403+
'sample' => [(clone $rule)->fileTypes(['jpeg', 'png', 'bmp'])],
1404+
]);
1405+
1406+
$expectedMessage = "The Sample file type must be 'jpeg', 'png', or 'bmp'";
1407+
$this->assertEquals($validation->errors()->first('sample'), $expectedMessage);
1408+
1409+
// Invalid file types with translation
1410+
$this->validator->setTranslation('or', 'atau');
1411+
$validation = $this->validator->validate([
1412+
'sample' => $file,
1413+
], [
1414+
'sample' => [(clone $rule)->fileTypes(['jpeg', 'png', 'bmp'])],
1415+
]);
1416+
1417+
$expectedMessage = "The Sample file type must be 'jpeg', 'png', atau 'bmp'";
1418+
$this->assertEquals($validation->errors()->first('sample'), $expectedMessage);
1419+
}
12801420
}

0 commit comments

Comments
 (0)