Skip to content

Commit 9536357

Browse files
authored
Merge pull request #22 from dipcode-software/bugfix/file-field-validator
Allow non-required file fields
2 parents c247a41 + 0ff86ea commit 9536357

File tree

5 files changed

+10
-10
lines changed

5 files changed

+10
-10
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/)
55
and this project adheres to [Semantic Versioning](http://semver.org/).
66

7-
## [2.0.4] - 2017-12-14
7+
## [2.0.4] - 2018-03-XX
88
### Fixed
99
- `data` and `files` were wrongly exposed on `Form` class. Changed visibility to private and added `getData` and `getFiles` methods.
10+
- Allow non-required file fields
1011

1112
## [2.0.3] - 2017-12-13
1213
### Added

src/Fields/FileField.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@ public function __construct(array $args = array())
3131

3232
public function validate($value)
3333
{
34-
if (is_null($value) && !$this->required) {
34+
if (0 == $value->size && !$this->required) {
3535
return;
3636
}
37-
if (is_null($value) && $this->required || 0 == $value->size && $this->required) {
37+
38+
if (0 == $value->size && $this->required) {
3839
throw new ValidationError($this->error_messages['required'], 'required');
3940
}
4041

@@ -54,9 +55,6 @@ public function validate($value)
5455

5556
public function toNative($value)
5657
{
57-
if (is_null($value)) {
58-
return null;
59-
}
6058
if (!is_array($value)) {
6159
throw new ValidationError(msg("INVALID_FILE"), 'invalid');
6260
}

src/Validators/FileTypeValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function __construct(array $valid_filetypes, $message = null)
2424

2525
public function __invoke($value)
2626
{
27-
if (!is_null($this->valid_filetypes) && !in_array($value->type, $this->valid_filetypes)) {
27+
if ($value->size > 0 && !is_null($this->valid_filetypes) && !in_array($value->type, $this->valid_filetypes)) {
2828
$message = msg($this->message, array(
2929
"valid_types" => implode(", ", $this->valid_filetypes),
3030
"type" => $value->type

tests/unit/Fields/FileFieldTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@ public function testConstruct()
2222

2323
/**
2424
* @expectedException PHPForm\Exceptions\ValidationError
25-
* @expectedExceptionMessage The submitted file is empty.
25+
* @expectedExceptionMessage This field is required.
2626
*/
2727
public function testValidateEmpty()
2828
{
2929
$data = array('size' => 0);
30-
$this->field->validate((object) $data);
30+
$field = new FileField(["max_size" => 20, "required" => true]);
31+
$field->validate((object) $data);
3132
}
3233

3334
/**

tests/unit/Validators/FileTypeValidatorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class FileTypeValidatorTest extends TestCase
1111

1212
public function setUp()
1313
{
14-
$this->data = (object) array('type' => 'image/png');
14+
$this->data = (object) array('size' => 10, 'type' => 'image/png');
1515
}
1616

1717
public function testValidType()

0 commit comments

Comments
 (0)