Skip to content

Commit

Permalink
File field set null value by default (#212)
Browse files Browse the repository at this point in the history
* Update yiisoft/test-support.
* File field set null value by default.
  • Loading branch information
vjik committed Jul 7, 2022
1 parent 9974ae2 commit 7e83954
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 23 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
@@ -1,4 +1,4 @@
# _____ Change Log
# Yii Form Change Log

## 1.0.0 under development

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -32,7 +32,7 @@
"roave/infection-static-analysis-plugin": "^1.18",
"spatie/phpunit-watcher": "^1.23",
"vimeo/psalm": "^4.23",
"yiisoft/test-support": " ^1.4"
"yiisoft/test-support": " ^3.0"
},
"autoload": {
"psr-4": {
Expand Down
22 changes: 9 additions & 13 deletions src/Field/File.php
Expand Up @@ -4,7 +4,6 @@

namespace Yiisoft\Form\Field;

use InvalidArgumentException;
use Stringable;
use Yiisoft\Form\Field\Base\EnrichmentFromRules\EnrichmentFromRulesInterface;
use Yiisoft\Form\Field\Base\EnrichmentFromRules\EnrichmentFromRulesTrait;
Expand All @@ -28,6 +27,7 @@ final class File extends InputField implements EnrichmentFromRulesInterface, Val

private bool|float|int|string|Stringable|null $uncheckValue = null;
private array $uncheckInputAttributes = [];
private string|Stringable|null $value = null;

/**
* The accept attribute value is a string that defines the file types the file input should accept. This string is
Expand Down Expand Up @@ -153,6 +153,13 @@ public function addUncheckInputAttributes(array $attributes): self
return $new;
}

public function value(string|Stringable|null $value): self
{
$new = clone $this;
$new->value = $value;
return $new;
}

/**
* @psalm-suppress MixedAssignment,MixedArgument Remove after fix https://github.com/yiisoft/validator/issues/225
*/
Expand All @@ -177,20 +184,9 @@ protected function beforeRender(): void

protected function generateInput(): string
{
$value = $this->getFormAttributeValue();

if (!is_string($value)
&& $value !== null
&& (!$value instanceof Stringable)
) {
throw new InvalidArgumentException(
'File field requires a string, Stringable or null value.'
);
}

$inputAttributes = $this->getInputAttributes();

$tag = Html::file($this->getInputName(), $value, $inputAttributes);
$tag = Html::file($this->getInputName(), $this->value, $inputAttributes);
if ($this->uncheckValue !== null) {
$tag = $tag->uncheckValue($this->uncheckValue);
if (!empty($this->uncheckInputAttributes)) {
Expand Down
21 changes: 14 additions & 7 deletions tests/Field/FileTest.php
Expand Up @@ -4,7 +4,6 @@

namespace Yiisoft\Form\Tests\Field;

use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use Yiisoft\Form\Field\File;
use Yiisoft\Form\Tests\Support\Form\FileForm;
Expand Down Expand Up @@ -265,14 +264,21 @@ public function testEnrichmentFromRulesWithWhen(): void
$this->assertSame($expected, $result);
}

public function testInvalidValue(): void
public function testValue(): void
{
$field = File::widget()
->formAttribute(new FileForm(), 'age');
$result = File::widget()
->hideLabel()
->formAttribute(new FileForm(), 'photo')
->value('test')
->render();

$expected = <<<HTML
<div>
<input type="file" id="fileform-photo" name="FileForm[photo]" value="test">
</div>
HTML;

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('File field requires a string, Stringable or null value.');
$field->render();
$this->assertSame($expected, $result);
}

public function testImmutability(): void
Expand All @@ -289,5 +295,6 @@ public function testImmutability(): void
$this->assertNotSame($field, $field->uncheckValue(null));
$this->assertNotSame($field, $field->uncheckInputAttributes([]));
$this->assertNotSame($field, $field->addUncheckInputAttributes([]));
$this->assertNotSame($field, $field->value(null));
}
}
1 change: 0 additions & 1 deletion tests/Support/Form/FileForm.php
Expand Up @@ -12,7 +12,6 @@ final class FileForm extends FormModel
private ?string $avatar = null;
private ?string $image = null;
private ?string $photo = null;
private ?int $age = 42;

public function getRules(): array
{
Expand Down

0 comments on commit 7e83954

Please sign in to comment.