diff --git a/CHANGELOG.md b/CHANGELOG.md index b3356eb..1688607 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,9 +4,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). -## [2.0.4] - 2017-12-14 +## [2.0.4] - 2018-03-XX ### Fixed - `data` and `files` were wrongly exposed on `Form` class. Changed visibility to private and added `getData` and `getFiles` methods. + - Allow non-required file fields ## [2.0.3] - 2017-12-13 ### Added diff --git a/src/Fields/FileField.php b/src/Fields/FileField.php index 251745b..77ad1a0 100644 --- a/src/Fields/FileField.php +++ b/src/Fields/FileField.php @@ -31,10 +31,11 @@ public function __construct(array $args = array()) public function validate($value) { - if (is_null($value) && !$this->required) { + if (0 == $value->size && !$this->required) { return; } - if (is_null($value) && $this->required || 0 == $value->size && $this->required) { + + if (0 == $value->size && $this->required) { throw new ValidationError($this->error_messages['required'], 'required'); } @@ -54,9 +55,6 @@ public function validate($value) public function toNative($value) { - if (is_null($value)) { - return null; - } if (!is_array($value)) { throw new ValidationError(msg("INVALID_FILE"), 'invalid'); } diff --git a/src/Validators/FileTypeValidator.php b/src/Validators/FileTypeValidator.php index 39183d6..13e4b58 100644 --- a/src/Validators/FileTypeValidator.php +++ b/src/Validators/FileTypeValidator.php @@ -24,7 +24,7 @@ public function __construct(array $valid_filetypes, $message = null) public function __invoke($value) { - if (!is_null($this->valid_filetypes) && !in_array($value->type, $this->valid_filetypes)) { + if ($value->size > 0 && !is_null($this->valid_filetypes) && !in_array($value->type, $this->valid_filetypes)) { $message = msg($this->message, array( "valid_types" => implode(", ", $this->valid_filetypes), "type" => $value->type diff --git a/tests/unit/Fields/FileFieldTest.php b/tests/unit/Fields/FileFieldTest.php index 2a5dd5f..caf3308 100644 --- a/tests/unit/Fields/FileFieldTest.php +++ b/tests/unit/Fields/FileFieldTest.php @@ -22,12 +22,13 @@ public function testConstruct() /** * @expectedException PHPForm\Exceptions\ValidationError - * @expectedExceptionMessage The submitted file is empty. + * @expectedExceptionMessage This field is required. */ public function testValidateEmpty() { $data = array('size' => 0); - $this->field->validate((object) $data); + $field = new FileField(["max_size" => 20, "required" => true]); + $field->validate((object) $data); } /** diff --git a/tests/unit/Validators/FileTypeValidatorTest.php b/tests/unit/Validators/FileTypeValidatorTest.php index e4fe483..b410a9a 100644 --- a/tests/unit/Validators/FileTypeValidatorTest.php +++ b/tests/unit/Validators/FileTypeValidatorTest.php @@ -11,7 +11,7 @@ class FileTypeValidatorTest extends TestCase public function setUp() { - $this->data = (object) array('type' => 'image/png'); + $this->data = (object) array('size' => 10, 'type' => 'image/png'); } public function testValidType()