Skip to content

Commit

Permalink
Improve invalid messages for 'in', 'not_in', 'mimes', and 'uploaded_f…
Browse files Browse the repository at this point in the history
…iles' rules
  • Loading branch information
emsifa committed Nov 27, 2018
1 parent bb8c375 commit 0db9279
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 7 deletions.
12 changes: 9 additions & 3 deletions src/Rules/In.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
10 changes: 8 additions & 2 deletions src/Rules/Mimes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
9 changes: 8 additions & 1 deletion src/Rules/NotIn.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
10 changes: 9 additions & 1 deletion src/Rules/UploadedFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -150,13 +155,15 @@ 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;
}
}

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;
}
}
Expand All @@ -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;
}
}
Expand Down
140 changes: 140 additions & 0 deletions tests/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 0db9279

Please sign in to comment.