Skip to content

Commit

Permalink
Add to textarea stringable and array of strings value support (#360)
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik authored Aug 23, 2024
1 parent 16fefb3 commit 0e34d0e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"require": {
"php": "^8.1",
"yiisoft/friendly-exception": "^1.0",
"yiisoft/html": "^3.5",
"yiisoft/html": "^3.6",
"yiisoft/widget": "^2.2"
},
"require-dev": {
Expand Down
15 changes: 13 additions & 2 deletions src/Field/Textarea.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Yiisoft\Form\Field;

use InvalidArgumentException;
use Stringable;
use Yiisoft\Form\Field\Base\EnrichFromValidationRules\EnrichFromValidationRulesInterface;
use Yiisoft\Form\Field\Base\EnrichFromValidationRules\EnrichFromValidationRulesTrait;
use Yiisoft\Form\Field\Base\InputField;
Expand Down Expand Up @@ -228,8 +229,15 @@ protected function generateInput(): string
{
$value = $this->getValue();

if (!is_string($value) && $value !== null) {
throw new InvalidArgumentException('Textarea field requires a string or null value.');
if (!(
is_string($value)
|| is_array($value)
|| $value instanceof Stringable
|| $value === null
)) {
throw new InvalidArgumentException(
'Textarea field requires a string, a stringable object, an array of strings or null value.'
);
}

/** @psalm-suppress MixedArgument We guess that enrichment contain correct values. */
Expand All @@ -238,6 +246,9 @@ protected function generateInput(): string
$this->getInputAttributes()
);

/**
* @var string|string[]|Stringable|null $value We guess that array value is array of strings.
*/
return Html::textarea($this->getName(), $value, $textareaAttributes)->render();
}

Expand Down
23 changes: 22 additions & 1 deletion tests/Field/TextareaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Yiisoft\Form\PureField\InputData;
use Yiisoft\Form\Tests\Support\NullValidationRulesEnricher;
use Yiisoft\Form\Tests\Support\RequiredValidationRulesEnricher;
use Yiisoft\Form\Tests\Support\StringableObject;
use Yiisoft\Form\Tests\Support\StubValidationRulesEnricher;
use Yiisoft\Form\Theme\ThemeContainer;

Expand All @@ -34,6 +35,24 @@ public static function dataBase(): array
HTML,
new InputData('desc', id: 'test-id', label: 'Description'),
],
'stringable-value' => [
<<<HTML
<div>
<textarea name="desc">test</textarea>
</div>
HTML,
new InputData('desc', new StringableObject('test')),
],
'array-of-strings-value' => [
<<<HTML
<div>
<textarea name="desc">a
b
c</textarea>
</div>
HTML,
new InputData('desc', ['a', 'b', 'c']),
],
'input-valid-class' => [
<<<HTML
<div>
Expand Down Expand Up @@ -299,7 +318,9 @@ public function testInvalidValue(): void
$widget = Textarea::widget()->value(7);

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Textarea field requires a string or null value.');
$this->expectExceptionMessage(
'Textarea field requires a string, a stringable object, an array of strings or null value.'
);
$widget->render();
}

Expand Down

0 comments on commit 0e34d0e

Please sign in to comment.