diff --git a/src/Rules/In.php b/src/Rules/In.php index ae98076..1971ed5 100644 --- a/src/Rules/In.php +++ b/src/Rules/In.php @@ -2,13 +2,14 @@ namespace Rakit\Validation\Rules; +use Rakit\Validation\Helper; use Rakit\Validation\Rule; class In extends Rule { /** @var string */ - protected $message = "The :attribute is not allowing :value"; + protected $message = "The :attribute only allows :allowed_values"; /** @var bool */ protected $strict = false; @@ -49,7 +50,12 @@ public function check($value): bool { $this->requireParameters(['allowed_values']); - $allowed_values = $this->parameter('allowed_values'); - return in_array($value, $allowed_values, $this->strict); + $allowedValues = $this->parameter('allowed_values'); + + $or = $this->validation ? $this->validation->getTranslation('or') : 'or'; + $allowedValuesText = Helper::join(Helper::wraps($allowedValues, "'"), ', ', ", {$or} "); + $this->setParameterText('allowed_values', $allowedValuesText); + + return in_array($value, $allowedValues, $this->strict); } } diff --git a/src/Rules/Mimes.php b/src/Rules/Mimes.php index d2ce8f1..9abb5a8 100644 --- a/src/Rules/Mimes.php +++ b/src/Rules/Mimes.php @@ -2,15 +2,16 @@ namespace Rakit\Validation\Rules; -use Rakit\Validation\Rule; +use Rakit\Validation\Helper; use Rakit\Validation\MimeTypeGuesser; +use Rakit\Validation\Rule; class Mimes extends Rule { use Traits\FileTrait; /** @var string */ - protected $message = "The :attribute file type is not allowed"; + protected $message = "The :attribute file type must be :allowed_types"; /** @var string|int */ protected $maxSize = null; @@ -60,6 +61,11 @@ public function check($value): bool { $allowedTypes = $this->parameter('allowed_types'); + if ($allowedTypes) { + $or = $this->validation ? $this->validation->getTranslation('or') : 'or'; + $this->setParameterText('allowed_types', Helper::join(Helper::wraps($allowedTypes, "'"), ', ', ", {$or} ")); + } + // below is Required rule job if (!$this->isValueFromUploadedFiles($value) or $value['error'] == UPLOAD_ERR_NO_FILE) { return true; diff --git a/src/Rules/NotIn.php b/src/Rules/NotIn.php index 4f4ad5e..f70275a 100644 --- a/src/Rules/NotIn.php +++ b/src/Rules/NotIn.php @@ -2,13 +2,14 @@ namespace Rakit\Validation\Rules; +use Rakit\Validation\Helper; use Rakit\Validation\Rule; class NotIn extends Rule { /** @var string */ - protected $message = "The :attribute is not allowing :value"; + protected $message = "The :attribute is not allowing :disallowed_values"; /** @var bool */ protected $strict = false; @@ -48,7 +49,13 @@ public function strict($strict = true) public function check($value): bool { $this->requireParameters(['disallowed_values']); + $disallowedValues = (array) $this->parameter('disallowed_values'); + + $and = $this->validation ? $this->validation->getTranslation('and') : 'and'; + $disallowedValuesText = Helper::join(Helper::wraps($disallowedValues, "'"), ', ', ", {$and} "); + $this->setParameterText('disallowed_values', $disallowedValuesText); + return !in_array($value, $disallowedValues, $this->strict); } } diff --git a/src/Rules/UploadedFile.php b/src/Rules/UploadedFile.php index eb530b0..ce5830e 100644 --- a/src/Rules/UploadedFile.php +++ b/src/Rules/UploadedFile.php @@ -12,7 +12,7 @@ class UploadedFile extends Rule implements BeforeValidate use Traits\FileTrait, Traits\SizeTrait; /** @var string */ - protected $message = "The :attribute is not valid"; + protected $message = "The :attribute is not valid uploaded file"; /** @var string|int */ protected $maxSize = null; @@ -133,6 +133,11 @@ public function check($value): bool $maxSize = $this->parameter('max_size'); $allowedTypes = $this->parameter('allowed_types'); + if ($allowedTypes) { + $or = $this->validation ? $this->validation->getTranslation('or') : 'or'; + $this->setParameterText('allowed_types', Helper::join(Helper::wraps($allowedTypes, "'"), ', ', ", {$or} ")); + } + // below is Required rule job if (!$this->isValueFromUploadedFiles($value) or $value['error'] == UPLOAD_ERR_NO_FILE) { return true; @@ -150,6 +155,7 @@ public function check($value): bool if ($minSize) { $bytesMinSize = $this->getBytesSize($minSize); if ($value['size'] < $bytesMinSize) { + $this->setMessage('The :attribute file is too small, minimum size is :min_size'); return false; } } @@ -157,6 +163,7 @@ public function check($value): bool if ($maxSize) { $bytesMaxSize = $this->getBytesSize($maxSize); if ($value['size'] > $bytesMaxSize) { + $this->setMessage('The :attribute file is too large, maximum size is :max_size'); return false; } } @@ -167,6 +174,7 @@ public function check($value): bool unset($guesser); if (!in_array($ext, $allowedTypes)) { + $this->setMessage('The :attribute file type must be :allowed_types'); return false; } } diff --git a/tests/ValidatorTest.php b/tests/ValidatorTest.php index bfdf5a9..6bde12f 100644 --- a/tests/ValidatorTest.php +++ b/tests/ValidatorTest.php @@ -1277,4 +1277,144 @@ public function testGetInvalidData() $this->assertFalse(isset($stuffs['one'])); $this->assertFalse(isset($stuffs['two'])); } + + public function testRuleInInvalidMessages() + { + $validation = $this->validator->validate([ + 'number' => 1 + ], [ + 'number' => 'in:7,8,9', + ]); + + $this->assertEquals($validation->errors()->first('number'), "The Number only allows '7', '8', or '9'"); + + // Using translation + $this->validator->setTranslation('or', 'atau'); + + $validation = $this->validator->validate([ + 'number' => 1 + ], [ + 'number' => 'in:7,8,9', + ]); + + $this->assertEquals($validation->errors()->first('number'), "The Number only allows '7', '8', atau '9'"); + } + + public function testRuleNotInInvalidMessages() + { + $validation = $this->validator->validate([ + 'number' => 1 + ], [ + 'number' => 'not_in:1,2,3', + ]); + + $this->assertEquals($validation->errors()->first('number'), "The Number is not allowing '1', '2', and '3'"); + + // Using translation + $this->validator->setTranslation('and', 'dan'); + + $validation = $this->validator->validate([ + 'number' => 1 + ], [ + 'number' => 'not_in:1,2,3', + ]); + + $this->assertEquals($validation->errors()->first('number'), "The Number is not allowing '1', '2', dan '3'"); + } + + public function testRuleMimesInvalidMessages() + { + $file = [ + 'name' => 'sample.txt', + 'type' => 'plain/text', + 'tmp_name' => __FILE__, + 'size' => 1000, + 'error' => UPLOAD_ERR_OK, + ]; + + $validation = $this->validator->validate([ + 'sample' => $file, + ], [ + 'sample' => 'mimes:jpeg,png,bmp', + ]); + + $expectedMessage = "The Sample file type must be 'jpeg', 'png', or 'bmp'"; + $this->assertEquals($validation->errors()->first('sample'), $expectedMessage); + + // Using translation + $this->validator->setTranslation('or', 'atau'); + + $validation = $this->validator->validate([ + 'sample' => $file, + ], [ + 'sample' => 'mimes:jpeg,png,bmp', + ]); + + $expectedMessage = "The Sample file type must be 'jpeg', 'png', atau 'bmp'"; + $this->assertEquals($validation->errors()->first('sample'), $expectedMessage); + } + + public function testRuleUploadedFileInvalidMessages() + { + $file = [ + 'name' => 'sample.txt', + 'type' => 'plain/text', + 'tmp_name' => __FILE__, + 'size' => 1024 * 1024 * 2, // 2M + 'error' => UPLOAD_ERR_OK, + ]; + + $rule = $this->getMockedUploadedFileRule(); + + // Invalid uploaded file (!is_uploaded_file($file['tmp_name'])) + $validation = $this->validator->validate([ + 'sample' => $file, + ], [ + 'sample' => 'uploaded_file', + ]); + + $expectedMessage = "The Sample is not valid uploaded file"; + $this->assertEquals($validation->errors()->first('sample'), $expectedMessage); + + // Invalid min size + $validation = $this->validator->validate([ + 'sample' => $file, + ], [ + 'sample' => [(clone $rule)->minSize('3M')], + ]); + + $expectedMessage = "The Sample file is too small, minimum size is 3M"; + $this->assertEquals($validation->errors()->first('sample'), $expectedMessage); + + // Invalid max size + $validation = $this->validator->validate([ + 'sample' => $file, + ], [ + 'sample' => [(clone $rule)->maxSize('1M')], + ]); + + $expectedMessage = "The Sample file is too large, maximum size is 1M"; + $this->assertEquals($validation->errors()->first('sample'), $expectedMessage); + + // Invalid file types + $validation = $this->validator->validate([ + 'sample' => $file, + ], [ + 'sample' => [(clone $rule)->fileTypes(['jpeg', 'png', 'bmp'])], + ]); + + $expectedMessage = "The Sample file type must be 'jpeg', 'png', or 'bmp'"; + $this->assertEquals($validation->errors()->first('sample'), $expectedMessage); + + // Invalid file types with translation + $this->validator->setTranslation('or', 'atau'); + $validation = $this->validator->validate([ + 'sample' => $file, + ], [ + 'sample' => [(clone $rule)->fileTypes(['jpeg', 'png', 'bmp'])], + ]); + + $expectedMessage = "The Sample file type must be 'jpeg', 'png', atau 'bmp'"; + $this->assertEquals($validation->errors()->first('sample'), $expectedMessage); + } }