Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 3 additions & 5 deletions src/Fields/FileField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand All @@ -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');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Validators/FileTypeValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions tests/unit/Fields/FileFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/Validators/FileTypeValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down