From 08a03d9756761c1327a586eee149201ff38f26de Mon Sep 17 00:00:00 2001 From: David Grudl Date: Sun, 12 May 2024 14:46:22 +0200 Subject: [PATCH] UploadControl: added setNullable() --- src/Forms/Controls/UploadControl.php | 38 +++++++++++++++---- .../Controls.UploadControl.loadData.phpt | 26 +++++++++++++ 2 files changed, 57 insertions(+), 7 deletions(-) diff --git a/src/Forms/Controls/UploadControl.php b/src/Forms/Controls/UploadControl.php index 1e807b0c1..3a13871ed 100644 --- a/src/Forms/Controls/UploadControl.php +++ b/src/Forms/Controls/UploadControl.php @@ -28,6 +28,8 @@ class UploadControl extends BaseControl /** @deprecated use UploadControl::Valid */ public const VALID = self::Valid; + private bool $nullable = false; + public function __construct(string|Stringable|null $label = null, bool $multiple = false) { @@ -55,7 +57,6 @@ public function __construct(string|Stringable|null $label = null, bool $multiple public function loadHttpData(): void { $this->value = $this->getHttpData(Form::DataFile); - $this->value ??= new FileUpload(null); } @@ -75,25 +76,48 @@ public function setValue($value) } + public function getValue(): FileUpload|array|null + { + return $this->value ?? ($this->nullable ? null : new FileUpload(null)); + } + + /** * Has been any file uploaded? */ public function isFilled(): bool { - return $this->value instanceof FileUpload - ? $this->value->getError() !== UPLOAD_ERR_NO_FILE // ignore null object - : (bool) $this->value; + return (bool) $this->value; + } + + + /** + * Sets whether getValue() returns null instead of FileUpload with error UPLOAD_ERR_NO_FILE. + */ + public function setNullable(bool $value = true): static + { + $this->nullable = $value; + return $this; + } + + + public function isNullable(): bool + { + return $this->nullable; } /** * Have been all files successfully uploaded? + * @internal */ public function isOk(): bool { - return $this->value instanceof FileUpload - ? $this->value->isOk() - : $this->value && Arrays::every($this->value, fn(FileUpload $upload): bool => $upload->isOk()); + return match (true) { + !$this->value => false, + is_array($this->value) => Arrays::every($this->value, fn(FileUpload $upload): bool => $upload->isOk()), + default => $this->value->isOk(), + }; } diff --git a/tests/Forms/Controls.UploadControl.loadData.phpt b/tests/Forms/Controls.UploadControl.loadData.phpt index 73c877511..b43a5d71f 100644 --- a/tests/Forms/Controls.UploadControl.loadData.phpt +++ b/tests/Forms/Controls.UploadControl.loadData.phpt @@ -146,6 +146,32 @@ test('empty data', function () { }); +test('missing data + nullable', function () { + $form = new Form; + $input = $form->addMultiUpload('empty') + ->setNullable() // has no effect + ->setRequired(); + + Assert::false($form->isValid()); + Assert::equal([], $input->getValue()); + Assert::false($input->isFilled()); + Assert::false($input->isOk()); +}); + + +test('empty data + nullable', function () { + $form = new Form; + $input = $form->addUpload('missing') + ->setNullable() + ->setRequired(); + + Assert::false($form->isValid()); + Assert::null($input->getValue()); + Assert::false($input->isFilled()); + Assert::false($input->isOk()); +}); + + test('malformed data', function () { $form = new Form; $input = $form->addUpload('invalid1');