From 5d866f15b2cba1941794fa4f271a9ec520c9b9aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mor=C3=A1vek?= Date: Fri, 10 Apr 2020 20:28:28 +0200 Subject: [PATCH 1/2] Validator: do not require BaseControl instance for filled and blank validation --- src/Forms/Validator.php | 13 ++-- tests/Forms/Validator.customControl.phpt | 76 ++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 4 deletions(-) create mode 100644 tests/Forms/Validator.customControl.phpt diff --git a/src/Forms/Validator.php b/src/Forms/Validator.php index 11b838267..e6a832288 100644 --- a/src/Forms/Validator.php +++ b/src/Forms/Validator.php @@ -131,18 +131,23 @@ public static function validateStatic(IControl $control, bool $arg): bool /** * Is control filled? */ - public static function validateFilled(Controls\BaseControl $control): bool + public static function validateFilled(IControl $control): bool { - return $control->isFilled(); + if ($control instanceof Controls\BaseControl || method_exists($control, 'isFilled')) { + return $control->isFilled(); + } + + $value = $control->getValue(); + return $value !== null && $value !== [] && $value !== ''; } /** * Is control not filled? */ - public static function validateBlank(Controls\BaseControl $control): bool + public static function validateBlank(IControl $control): bool { - return !$control->isFilled(); + return !static::validateFilled($control); } diff --git a/tests/Forms/Validator.customControl.phpt b/tests/Forms/Validator.customControl.phpt new file mode 100644 index 000000000..12b7fc2ec --- /dev/null +++ b/tests/Forms/Validator.customControl.phpt @@ -0,0 +1,76 @@ +value = $value; + } + + + public function setValue($value) + { + $this->value = $value; + } + + + public function getValue() + { + return $this->value; + } + + + public function validate(): void + { + } + + + public function getErrors(): array + { + return []; + } + + + public function isOmitted(): bool + { + return false; + } + +} + + +test(function () { // filled, blank + $input = new CustomControl(''); + Assert::false(Validator::validateFilled($input)); + Assert::true(Validator::validateBlank($input)); + + $input = new CustomControl(null); + Assert::false(Validator::validateFilled($input)); + Assert::true(Validator::validateBlank($input)); + + $input = new CustomControl([]); + Assert::false(Validator::validateFilled($input)); + Assert::true(Validator::validateBlank($input)); + + $input = new CustomControl(42); + Assert::true(Validator::validateFilled($input)); + Assert::false(Validator::validateBlank($input)); +}); From 237c9fec5c3491d5ad7445323dd2fd4fa55bdc50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mor=C3=A1vek?= Date: Fri, 10 Apr 2020 20:41:27 +0200 Subject: [PATCH 2/2] Validator: do not require UploadControl instance for file upload related validations --- src/Forms/Validator.php | 9 +++-- tests/Forms/Validator.customControl.phpt | 46 ++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/src/Forms/Validator.php b/src/Forms/Validator.php index e6a832288..1d76848da 100644 --- a/src/Forms/Validator.php +++ b/src/Forms/Validator.php @@ -323,9 +323,10 @@ public static function validateFloat(IControl $control): bool /** * Is file size in limit? */ - public static function validateFileSize(Controls\UploadControl $control, $limit): bool + public static function validateFileSize(IControl $control, $limit): bool { foreach (static::toArray($control->getValue()) as $file) { + Validators::assert($file, Nette\Http\FileUpload::class, 'control value'); if ($file->getSize() > $limit || $file->getError() === UPLOAD_ERR_INI_SIZE) { return false; } @@ -338,10 +339,11 @@ public static function validateFileSize(Controls\UploadControl $control, $limit) * Has file specified mime type? * @param string|string[] $mimeType */ - public static function validateMimeType(Controls\UploadControl $control, $mimeType): bool + public static function validateMimeType(IControl $control, $mimeType): bool { $mimeTypes = is_array($mimeType) ? $mimeType : explode(',', $mimeType); foreach (static::toArray($control->getValue()) as $file) { + Validators::assert($file, Nette\Http\FileUpload::class, 'control value'); $type = strtolower($file->getContentType()); if (!in_array($type, $mimeTypes, true) && !in_array(preg_replace('#/.*#', '/*', $type), $mimeTypes, true)) { return false; @@ -354,9 +356,10 @@ public static function validateMimeType(Controls\UploadControl $control, $mimeTy /** * Is file image? */ - public static function validateImage(Controls\UploadControl $control): bool + public static function validateImage(IControl $control): bool { foreach (static::toArray($control->getValue()) as $file) { + Validators::assert($file, Nette\Http\FileUpload::class, 'control value'); if (!$file->isImage()) { return false; } diff --git a/tests/Forms/Validator.customControl.phpt b/tests/Forms/Validator.customControl.phpt index 12b7fc2ec..1325095c1 100644 --- a/tests/Forms/Validator.customControl.phpt +++ b/tests/Forms/Validator.customControl.phpt @@ -8,6 +8,8 @@ declare(strict_types=1); use Nette\Forms\Form; use Nette\Forms\Validator; +use Nette\Http\FileUpload; +use Nette\Utils\AssertionException; use Tester\Assert; @@ -74,3 +76,47 @@ test(function () { // filled, blank Assert::true(Validator::validateFilled($input)); Assert::false(Validator::validateBlank($input)); }); + + +test(function () { // file upload related validators + $input = new CustomControl(new FileUpload([ + 'name' => 'foo', + 'size' => 1, + 'tmp_name' => __FILE__, + 'error' => UPLOAD_ERR_OK + ])); + Assert::true(Validator::validateFileSize($input, 42)); + Assert::true(Validator::validateMimeType($input, ['text/x-php'])); + Assert::false(Validator::validateImage($input)); + + $input = new CustomControl(new FileUpload([ + 'name' => 'foo', + 'size' => 100, + 'tmp_name' => __DIR__ . '/files/logo.gif', + 'error' => UPLOAD_ERR_OK + ])); + Assert::false(Validator::validateFileSize($input, 42)); + Assert::false(Validator::validateMimeType($input, ['text/x-php'])); + Assert::true(Validator::validateImage($input)); + + Assert::exception( + function () : void { + Assert::false(Validator::validateFileSize(new CustomControl('foo'), 42)); + }, + AssertionException::class + ); + + Assert::exception( + function () : void { + Assert::false(Validator::validateMimeType(new CustomControl('foo'), ['plain/text'])); + }, + AssertionException::class + ); + + Assert::exception( + function () : void { + Assert::false(Validator::validateImage(new CustomControl('foo'))); + }, + AssertionException::class + ); +});