Skip to content

Commit

Permalink
UploadControl: added setNullable()
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Aug 5, 2024
1 parent c6d0c29 commit 4ead20a
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 7 deletions.
38 changes: 31 additions & 7 deletions src/Forms/Controls/UploadControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
}


Expand All @@ -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(),
};
}


Expand Down
26 changes: 26 additions & 0 deletions tests/Forms/Controls.UploadControl.loadData.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 4ead20a

Please sign in to comment.