Skip to content

Commit f874222

Browse files
committed
UploadControl: added setNullable()
1 parent 2ceb5bd commit f874222

File tree

2 files changed

+57
-7
lines changed

2 files changed

+57
-7
lines changed

src/Forms/Controls/UploadControl.php

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ class UploadControl extends BaseControl
2828
/** @deprecated use UploadControl::Valid */
2929
public const VALID = self::Valid;
3030

31+
private bool $nullable = false;
32+
3133

3234
public function __construct(string|Stringable|null $label = null, bool $multiple = false)
3335
{
@@ -55,7 +57,6 @@ public function __construct(string|Stringable|null $label = null, bool $multiple
5557
public function loadHttpData(): void
5658
{
5759
$this->value = $this->getHttpData(Form::DataFile);
58-
$this->value ??= new FileUpload(null);
5960
}
6061

6162

@@ -75,25 +76,48 @@ public function setValue($value)
7576
}
7677

7778

79+
public function getValue(): FileUpload|array|null
80+
{
81+
return $this->value ?? ($this->nullable ? null : new FileUpload(null));
82+
}
83+
84+
7885
/**
7986
* Has been any file uploaded?
8087
*/
8188
public function isFilled(): bool
8289
{
83-
return $this->value instanceof FileUpload
84-
? $this->value->getError() !== UPLOAD_ERR_NO_FILE // ignore null object
85-
: (bool) $this->value;
90+
return (bool) $this->value;
91+
}
92+
93+
94+
/**
95+
* Sets whether getValue() returns null instead of FileUpload with error UPLOAD_ERR_NO_FILE.
96+
*/
97+
public function setNullable(bool $value = true): static
98+
{
99+
$this->nullable = $value;
100+
return $this;
101+
}
102+
103+
104+
public function isNullable(): bool
105+
{
106+
return $this->nullable;
86107
}
87108

88109

89110
/**
90111
* Have been all files successfully uploaded?
112+
* @internal
91113
*/
92114
public function isOk(): bool
93115
{
94-
return $this->value instanceof FileUpload
95-
? $this->value->isOk()
96-
: $this->value && Arrays::every($this->value, fn(FileUpload $upload): bool => $upload->isOk());
116+
return $this->value &&
117+
Arrays::every(
118+
$this->value instanceof FileUpload ? [$this->value] : $this->value,
119+
fn(FileUpload $upload): bool => $upload->isOk()
120+
);
97121
}
98122

99123

tests/Forms/Controls.UploadControl.loadData.phpt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,32 @@ test('empty data', function () {
146146
});
147147

148148

149+
test('missing data + nullable', function () {
150+
$form = new Form;
151+
$input = $form->addMultiUpload('empty')
152+
->setNullable() // has no effect
153+
->setRequired();
154+
155+
Assert::false($form->isValid());
156+
Assert::equal([], $input->getValue());
157+
Assert::false($input->isFilled());
158+
Assert::false($input->isOk());
159+
});
160+
161+
162+
test('empty data + nullable', function () {
163+
$form = new Form;
164+
$input = $form->addUpload('missing')
165+
->setNullable()
166+
->setRequired();
167+
168+
Assert::false($form->isValid());
169+
Assert::null($input->getValue());
170+
Assert::false($input->isFilled());
171+
Assert::false($input->isOk());
172+
});
173+
174+
149175
test('malformed data', function () {
150176
$form = new Form;
151177
$input = $form->addUpload('invalid1');

0 commit comments

Comments
 (0)