diff --git a/src/Forms/Validator.php b/src/Forms/Validator.php index 11b838267..1d76848da 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); } @@ -318,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; } @@ -333,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; @@ -349,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 new file mode 100644 index 000000000..1325095c1 --- /dev/null +++ b/tests/Forms/Validator.customControl.phpt @@ -0,0 +1,122 @@ +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)); +}); + + +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 + ); +});