From 9fc69be96b72f4e809c63e0c22efba8b76215fe9 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Sat, 25 Nov 2023 20:42:40 +0300 Subject: [PATCH 1/4] Add methods `name()` and `value()` for input fields --- .../CustomNameAndValueForInputDataTrait.php | 22 +++++ .../Base/InputData/FormModelInputData.php | 44 ++++++++- .../Base/InputData/InputDataInterface.php | 10 ++ src/Field/Base/InputData/PureInputData.php | 14 +++ src/Field/Base/InputField.php | 2 + src/Field/CheckboxList.php | 2 + src/Field/File.php | 10 +- src/Field/RadioList.php | 2 + tests/Field/FileTest.php | 18 ---- tests/Field/TextTest.php | 91 ++++++++++++++++++- 10 files changed, 182 insertions(+), 33 deletions(-) create mode 100644 src/Field/Base/InputData/CustomNameAndValueForInputDataTrait.php diff --git a/src/Field/Base/InputData/CustomNameAndValueForInputDataTrait.php b/src/Field/Base/InputData/CustomNameAndValueForInputDataTrait.php new file mode 100644 index 00000000..9b4393e9 --- /dev/null +++ b/src/Field/Base/InputData/CustomNameAndValueForInputDataTrait.php @@ -0,0 +1,22 @@ +inputData = $this->getInputData()->withName($name); + return $new; + } + + final public function value(mixed $value): static + { + $new = clone $this; + $new->inputData = $this->getInputData()->withValue($value); + return $new; + } +} diff --git a/src/Field/Base/InputData/FormModelInputData.php b/src/Field/Base/InputData/FormModelInputData.php index 90cb1b68..11bcb03e 100644 --- a/src/Field/Base/InputData/FormModelInputData.php +++ b/src/Field/Base/InputData/FormModelInputData.php @@ -22,6 +22,11 @@ final class FormModelInputData implements InputDataInterface */ private ?iterable $validationRules = null; + private bool $useCustomName = false; + private ?string $customName = null; + private bool $useCustomValue = false; + private mixed $customValue = null; + public function __construct( private FormModelInterface $model, private string $property, @@ -48,10 +53,14 @@ public function getValidationRules(): iterable * * @throws InvalidArgumentException If the attribute name contains non-word characters or empty form name for * tabular inputs. - * @return string The generated input name. + * @return string|null The generated input name. */ - public function getName(): string + public function getName(): ?string { + if ($this->useCustomName) { + return $this->customName; + } + $data = $this->parseProperty($this->property); $formName = $this->model->getFormName(); @@ -74,6 +83,10 @@ public function getName(): string */ public function getValue(): mixed { + if ($this->useCustomValue) { + return $this->customValue; + } + $parsedName = $this->parseProperty($this->property); return $this->model->getAttributeValue($parsedName['name'] . $parsedName['suffix']); } @@ -102,11 +115,16 @@ public function getPlaceholder(): ?string * For example, if {@see getInputName()} returns `Post[content]`, this method will return `post-content`. * * @throws InvalidArgumentException If the attribute name contains non-word characters. - * @return string The generated input ID. + * @return string|null The generated input ID. */ - public function getId(): string + public function getId(): ?string { - $name = mb_strtolower($this->getName(), 'UTF-8'); + $name = $this->getName(); + if ($name === null) { + return null; + } + + $name = mb_strtolower($name, 'UTF-8'); return str_replace(['[]', '][', '[', ']', ' ', '.'], ['', '-', '-', '', '-', '-'], $name); } @@ -165,4 +183,20 @@ private function parseProperty(string $property): array 'suffix' => $matches[3], ]; } + + public function withName(?string $name): static + { + $new = clone $this; + $new->customName = $name; + $new->useCustomName = true; + return $new; + } + + public function withValue(mixed $value): static + { + $new = clone $this; + $new->customValue = $value; + $new->useCustomValue = true; + return $new; + } } diff --git a/src/Field/Base/InputData/InputDataInterface.php b/src/Field/Base/InputData/InputDataInterface.php index 053d937d..97f07488 100644 --- a/src/Field/Base/InputData/InputDataInterface.php +++ b/src/Field/Base/InputData/InputDataInterface.php @@ -35,4 +35,14 @@ public function getValidationRules(): iterable; * @psalm-return list */ public function getValidationErrors(): array; + + /** + * @psalm-immutable + */ + public function withName(?string $name): static; + + /** + * @psalm-immutable + */ + public function withValue(mixed $value): static; } diff --git a/src/Field/Base/InputData/PureInputData.php b/src/Field/Base/InputData/PureInputData.php index a34ee739..8d85ad97 100644 --- a/src/Field/Base/InputData/PureInputData.php +++ b/src/Field/Base/InputData/PureInputData.php @@ -56,4 +56,18 @@ public function getValidationErrors(): array { return []; } + + public function withName(?string $name): static + { + $new = clone $this; + $new->name = $name; + return $new; + } + + public function withValue(mixed $value): static + { + $new = clone $this; + $new->value = $value; + return $new; + } } diff --git a/src/Field/Base/InputField.php b/src/Field/Base/InputField.php index 56eed70a..a05bdea5 100644 --- a/src/Field/Base/InputField.php +++ b/src/Field/Base/InputField.php @@ -4,6 +4,7 @@ namespace Yiisoft\Form\Field\Base; +use Yiisoft\Form\Field\Base\InputData\CustomNameAndValueForInputDataTrait; use Yiisoft\Form\Field\Base\InputData\InputDataTrait; use Yiisoft\Form\Field\Part\Error; use Yiisoft\Form\Field\Part\Hint; @@ -13,6 +14,7 @@ abstract class InputField extends PartsField { use InputDataTrait; + use CustomNameAndValueForInputDataTrait; protected ?string $inputId = null; protected ?string $inputIdFromTag = null; diff --git a/src/Field/CheckboxList.php b/src/Field/CheckboxList.php index cd9dba8e..8a2b3abc 100644 --- a/src/Field/CheckboxList.php +++ b/src/Field/CheckboxList.php @@ -8,6 +8,7 @@ use InvalidArgumentException; use LogicException; use Stringable; +use Yiisoft\Form\Field\Base\InputData\CustomNameAndValueForInputDataTrait; use Yiisoft\Form\Field\Base\InputData\InputDataTrait; use Yiisoft\Form\Field\Base\PartsField; use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassInterface; @@ -26,6 +27,7 @@ final class CheckboxList extends PartsField implements ValidationClassInterface { use InputDataTrait; + use CustomNameAndValueForInputDataTrait; use ValidationClassTrait; private CheckboxListWidget $widget; diff --git a/src/Field/File.php b/src/Field/File.php index 4a85f2ee..edcbec57 100644 --- a/src/Field/File.php +++ b/src/Field/File.php @@ -27,7 +27,6 @@ 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 @@ -153,13 +152,6 @@ 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 */ @@ -183,7 +175,7 @@ protected function generateInput(): string { $inputAttributes = $this->getInputAttributes(); - $tag = Html::file($this->getInputData()->getName(), $this->value, $inputAttributes); + $tag = Html::file($this->getInputData()->getName(), attributes: $inputAttributes); if ($this->uncheckValue !== null) { $tag = $tag->uncheckValue($this->uncheckValue); if (!empty($this->uncheckInputAttributes)) { diff --git a/src/Field/RadioList.php b/src/Field/RadioList.php index f0a892f5..56bfbbad 100644 --- a/src/Field/RadioList.php +++ b/src/Field/RadioList.php @@ -8,6 +8,7 @@ use InvalidArgumentException; use LogicException; use Stringable; +use Yiisoft\Form\Field\Base\InputData\CustomNameAndValueForInputDataTrait; use Yiisoft\Form\Field\Base\InputData\InputDataTrait; use Yiisoft\Form\Field\Base\PartsField; use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassInterface; @@ -24,6 +25,7 @@ final class RadioList extends PartsField implements ValidationClassInterface { use InputDataTrait; + use CustomNameAndValueForInputDataTrait; use ValidationClassTrait; private RadioListWidget $widget; diff --git a/tests/Field/FileTest.php b/tests/Field/FileTest.php index bda9c465..8ab7c1d6 100644 --- a/tests/Field/FileTest.php +++ b/tests/Field/FileTest.php @@ -267,23 +267,6 @@ public function testEnrichmentFromRulesWithWhen(): void $this->assertSame($expected, $result); } - public function testValue(): void - { - $result = File::widget() - ->hideLabel() - ->inputData(new FormModelInputData(new FileForm(), 'photo')) - ->value('test') - ->render(); - - $expected = << - - - HTML; - - $this->assertSame($expected, $result); - } - public function testImmutability(): void { $field = File::widget(); @@ -298,6 +281,5 @@ 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)); } } diff --git a/tests/Field/TextTest.php b/tests/Field/TextTest.php index 06451639..e91225b5 100644 --- a/tests/Field/TextTest.php +++ b/tests/Field/TextTest.php @@ -41,7 +41,7 @@ public function testBase(): void $this->assertSame($expected, $result); } - public function testWithoutForm(): void + public function testEmpty(): void { $expected = << @@ -54,6 +54,95 @@ public function testWithoutForm(): void $this->assertSame($expected, $result); } + public function testCustomName(): void + { + $expected = << + + + HTML; + + $result = Text::widget()->name('the-name')->render(); + + $this->assertSame($expected, $result); + } + + public function testCustomNameAfterInputData(): void + { + $expected = << + + +
Input your full name.
+
Value cannot be blank.
+ + HTML; + + $result = Text::widget() + ->inputData(new FormModelInputData(TextForm::validated(), 'name')) + ->name('the-name') + ->render(); + + $this->assertSame($expected, $result); + } + + public function testCustomNameBeforeInputData(): void + { + $expected = << + + +
Input your full name.
+
Value cannot be blank.
+ + HTML; + + $result = Text::widget() + ->name('the-name') + ->inputData(new FormModelInputData(TextForm::validated(), 'name')) + ->render(); + + $this->assertSame($expected, $result); + } + + public function testCustomValueAfterInputData(): void + { + $expected = << + + +
Input your full name.
+
Value cannot be blank.
+ + HTML; + + $result = Text::widget() + ->inputData(new FormModelInputData(TextForm::validated(), 'name')) + ->value('42') + ->render(); + + $this->assertSame($expected, $result); + } + + public function testCustomValueBeforeInputData(): void + { + $expected = << + + +
Input your full name.
+
Value cannot be blank.
+ + HTML; + + $result = Text::widget() + ->value('42') + ->inputData(new FormModelInputData(TextForm::validated(), 'name')) + ->render(); + + $this->assertSame($expected, $result); + } + public function testInvalidValue(): void { $widget = Text::widget() From 737d0f57139108fa77012adb95391a8ecf4c086b Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Sat, 25 Nov 2023 17:43:17 +0000 Subject: [PATCH 2/4] Apply fixes from StyleCI --- src/Field/Base/InputField.php | 2 +- src/Field/CheckboxList.php | 2 +- src/Field/RadioList.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Field/Base/InputField.php b/src/Field/Base/InputField.php index a05bdea5..26abba80 100644 --- a/src/Field/Base/InputField.php +++ b/src/Field/Base/InputField.php @@ -13,8 +13,8 @@ abstract class InputField extends PartsField { - use InputDataTrait; use CustomNameAndValueForInputDataTrait; + use InputDataTrait; protected ?string $inputId = null; protected ?string $inputIdFromTag = null; diff --git a/src/Field/CheckboxList.php b/src/Field/CheckboxList.php index 8a2b3abc..4c4bb301 100644 --- a/src/Field/CheckboxList.php +++ b/src/Field/CheckboxList.php @@ -26,8 +26,8 @@ */ final class CheckboxList extends PartsField implements ValidationClassInterface { - use InputDataTrait; use CustomNameAndValueForInputDataTrait; + use InputDataTrait; use ValidationClassTrait; private CheckboxListWidget $widget; diff --git a/src/Field/RadioList.php b/src/Field/RadioList.php index 56bfbbad..2a78fdf5 100644 --- a/src/Field/RadioList.php +++ b/src/Field/RadioList.php @@ -24,8 +24,8 @@ */ final class RadioList extends PartsField implements ValidationClassInterface { - use InputDataTrait; use CustomNameAndValueForInputDataTrait; + use InputDataTrait; use ValidationClassTrait; private RadioListWidget $widget; From 27f7ac864e6349938be0d3ae7413dfc6dcfad18a Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Sun, 26 Nov 2023 10:54:56 +0300 Subject: [PATCH 3/4] improve --- src/Field/Base/DateTimeInputField.php | 4 +- .../CustomNameAndValueForInputDataTrait.php | 22 ------- .../Base/InputData/FormModelInputData.php | 29 --------- .../Base/InputData/InputDataInterface.php | 10 --- .../InputDataWithCustomNameAndValueTrait.php | 62 +++++++++++++++++++ src/Field/Base/InputData/PureInputData.php | 14 ----- src/Field/Base/InputField.php | 5 +- src/Field/Checkbox.php | 4 +- src/Field/CheckboxList.php | 9 ++- src/Field/Email.php | 4 +- src/Field/File.php | 2 +- src/Field/Hidden.php | 4 +- src/Field/Number.php | 4 +- src/Field/Password.php | 4 +- src/Field/RadioList.php | 9 ++- src/Field/Range.php | 4 +- src/Field/Select.php | 4 +- src/Field/Telephone.php | 4 +- src/Field/Text.php | 4 +- src/Field/Textarea.php | 4 +- src/Field/Url.php | 4 +- tests/Field/TextTest.php | 4 +- 22 files changed, 99 insertions(+), 115 deletions(-) delete mode 100644 src/Field/Base/InputData/CustomNameAndValueForInputDataTrait.php create mode 100644 src/Field/Base/InputData/InputDataWithCustomNameAndValueTrait.php diff --git a/src/Field/Base/DateTimeInputField.php b/src/Field/Base/DateTimeInputField.php index 6d8a71b4..ac98e28a 100644 --- a/src/Field/Base/DateTimeInputField.php +++ b/src/Field/Base/DateTimeInputField.php @@ -161,7 +161,7 @@ protected function beforeRender(): void final protected function generateInput(): string { - $value = $this->getInputData()->getValue(); + $value = $this->getValue(); if (!is_string($value) && $value !== null) { throw new InvalidArgumentException( @@ -172,7 +172,7 @@ final protected function generateInput(): string $inputAttributes = $this->getInputAttributes(); - return Html::input($this->getInputType(), $this->getInputData()->getName(), $value, $inputAttributes)->render(); + return Html::input($this->getInputType(), $this->getName(), $value, $inputAttributes)->render(); } abstract protected function getInputType(): string; diff --git a/src/Field/Base/InputData/CustomNameAndValueForInputDataTrait.php b/src/Field/Base/InputData/CustomNameAndValueForInputDataTrait.php deleted file mode 100644 index 9b4393e9..00000000 --- a/src/Field/Base/InputData/CustomNameAndValueForInputDataTrait.php +++ /dev/null @@ -1,22 +0,0 @@ -inputData = $this->getInputData()->withName($name); - return $new; - } - - final public function value(mixed $value): static - { - $new = clone $this; - $new->inputData = $this->getInputData()->withValue($value); - return $new; - } -} diff --git a/src/Field/Base/InputData/FormModelInputData.php b/src/Field/Base/InputData/FormModelInputData.php index 11bcb03e..4d3dcf9c 100644 --- a/src/Field/Base/InputData/FormModelInputData.php +++ b/src/Field/Base/InputData/FormModelInputData.php @@ -22,11 +22,6 @@ final class FormModelInputData implements InputDataInterface */ private ?iterable $validationRules = null; - private bool $useCustomName = false; - private ?string $customName = null; - private bool $useCustomValue = false; - private mixed $customValue = null; - public function __construct( private FormModelInterface $model, private string $property, @@ -57,10 +52,6 @@ public function getValidationRules(): iterable */ public function getName(): ?string { - if ($this->useCustomName) { - return $this->customName; - } - $data = $this->parseProperty($this->property); $formName = $this->model->getFormName(); @@ -83,10 +74,6 @@ public function getName(): ?string */ public function getValue(): mixed { - if ($this->useCustomValue) { - return $this->customValue; - } - $parsedName = $this->parseProperty($this->property); return $this->model->getAttributeValue($parsedName['name'] . $parsedName['suffix']); } @@ -183,20 +170,4 @@ private function parseProperty(string $property): array 'suffix' => $matches[3], ]; } - - public function withName(?string $name): static - { - $new = clone $this; - $new->customName = $name; - $new->useCustomName = true; - return $new; - } - - public function withValue(mixed $value): static - { - $new = clone $this; - $new->customValue = $value; - $new->useCustomValue = true; - return $new; - } } diff --git a/src/Field/Base/InputData/InputDataInterface.php b/src/Field/Base/InputData/InputDataInterface.php index 97f07488..053d937d 100644 --- a/src/Field/Base/InputData/InputDataInterface.php +++ b/src/Field/Base/InputData/InputDataInterface.php @@ -35,14 +35,4 @@ public function getValidationRules(): iterable; * @psalm-return list */ public function getValidationErrors(): array; - - /** - * @psalm-immutable - */ - public function withName(?string $name): static; - - /** - * @psalm-immutable - */ - public function withValue(mixed $value): static; } diff --git a/src/Field/Base/InputData/InputDataWithCustomNameAndValueTrait.php b/src/Field/Base/InputData/InputDataWithCustomNameAndValueTrait.php new file mode 100644 index 00000000..74b6a641 --- /dev/null +++ b/src/Field/Base/InputData/InputDataWithCustomNameAndValueTrait.php @@ -0,0 +1,62 @@ +inputData = $inputData; + $new->useCustomName = false; + $new->useCustomValue = false; + return $new; + } + + final protected function getInputData(): InputDataInterface + { + if ($this->inputData === null) { + $this->inputData = new PureInputData(); + } + + return $this->inputData; + } + + final public function name(?string $name): static + { + $new = clone $this; + $new->customName = $name; + $new->useCustomName = true; + return $new; + } + + final public function value(mixed $value): static + { + $new = clone $this; + $new->customValue = $value; + $new->useCustomValue = true; + return $new; + } + + final protected function getName(): ?string + { + return $this->useCustomName + ? $this->customName + : $this->getInputData()->getName(); + } + + final protected function getValue(): mixed + { + return $this->useCustomValue + ? $this->customValue + : $this->getInputData()->getValue(); + } +} diff --git a/src/Field/Base/InputData/PureInputData.php b/src/Field/Base/InputData/PureInputData.php index 8d85ad97..a34ee739 100644 --- a/src/Field/Base/InputData/PureInputData.php +++ b/src/Field/Base/InputData/PureInputData.php @@ -56,18 +56,4 @@ public function getValidationErrors(): array { return []; } - - public function withName(?string $name): static - { - $new = clone $this; - $new->name = $name; - return $new; - } - - public function withValue(mixed $value): static - { - $new = clone $this; - $new->value = $value; - return $new; - } } diff --git a/src/Field/Base/InputField.php b/src/Field/Base/InputField.php index 26abba80..548774dc 100644 --- a/src/Field/Base/InputField.php +++ b/src/Field/Base/InputField.php @@ -4,7 +4,7 @@ namespace Yiisoft\Form\Field\Base; -use Yiisoft\Form\Field\Base\InputData\CustomNameAndValueForInputDataTrait; +use Yiisoft\Form\Field\Base\InputData\InputDataWithCustomNameAndValueTrait; use Yiisoft\Form\Field\Base\InputData\InputDataTrait; use Yiisoft\Form\Field\Part\Error; use Yiisoft\Form\Field\Part\Hint; @@ -13,8 +13,7 @@ abstract class InputField extends PartsField { - use CustomNameAndValueForInputDataTrait; - use InputDataTrait; + use InputDataWithCustomNameAndValueTrait; protected ?string $inputId = null; protected ?string $inputIdFromTag = null; diff --git a/src/Field/Checkbox.php b/src/Field/Checkbox.php index f3305575..1c7a3527 100644 --- a/src/Field/Checkbox.php +++ b/src/Field/Checkbox.php @@ -215,7 +215,7 @@ public function uncheckValue(bool|float|int|string|Stringable|null $value): self protected function generateInput(): string { - $value = $this->getInputData()->getValue(); + $value = $this->getValue(); if (!is_bool($value) && !is_string($value) @@ -237,7 +237,7 @@ protected function generateInput(): string unset($inputAttributes['value']); $inputValue ??= '1'; - $checkbox = Html::checkbox($this->getInputData()->getName(), $inputValue, $inputAttributes); + $checkbox = Html::checkbox($this->getName(), $inputValue, $inputAttributes); $label = $this->inputLabel ?? $this->getInputData()->getLabel(); diff --git a/src/Field/CheckboxList.php b/src/Field/CheckboxList.php index 4c4bb301..b33d03d1 100644 --- a/src/Field/CheckboxList.php +++ b/src/Field/CheckboxList.php @@ -8,7 +8,7 @@ use InvalidArgumentException; use LogicException; use Stringable; -use Yiisoft\Form\Field\Base\InputData\CustomNameAndValueForInputDataTrait; +use Yiisoft\Form\Field\Base\InputData\InputDataWithCustomNameAndValueTrait; use Yiisoft\Form\Field\Base\InputData\InputDataTrait; use Yiisoft\Form\Field\Base\PartsField; use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassInterface; @@ -26,8 +26,7 @@ */ final class CheckboxList extends PartsField implements ValidationClassInterface { - use CustomNameAndValueForInputDataTrait; - use InputDataTrait; + use InputDataWithCustomNameAndValueTrait; use ValidationClassTrait; private CheckboxListWidget $widget; @@ -146,12 +145,12 @@ public function itemFormatter(?Closure $formatter): self protected function generateInput(): string { - $name = $this->getInputData()->getName(); + $name = $this->getName(); if (empty($name)) { throw new LogicException('"CheckboxList" field requires non-empty name.'); } - $value = $this->getInputData()->getValue(); + $value = $this->getValue(); $value ??= []; if (!is_iterable($value)) { diff --git a/src/Field/Email.php b/src/Field/Email.php index 4f4043fa..4babb7d9 100644 --- a/src/Field/Email.php +++ b/src/Field/Email.php @@ -239,7 +239,7 @@ protected function beforeRender(): void protected function generateInput(): string { - $value = $this->getInputData()->getValue(); + $value = $this->getValue(); if (!is_string($value) && $value !== null) { throw new InvalidArgumentException('Email field requires a string or null value.'); @@ -247,7 +247,7 @@ protected function generateInput(): string $attributes = $this->getInputAttributes(); - return Html::input('email', $this->getInputData()->getName(), $value, $attributes)->render(); + return Html::input('email', $this->getName(), $value, $attributes)->render(); } protected function prepareContainerAttributes(array &$attributes): void diff --git a/src/Field/File.php b/src/Field/File.php index edcbec57..874ba71d 100644 --- a/src/Field/File.php +++ b/src/Field/File.php @@ -175,7 +175,7 @@ protected function generateInput(): string { $inputAttributes = $this->getInputAttributes(); - $tag = Html::file($this->getInputData()->getName(), attributes: $inputAttributes); + $tag = Html::file($this->getName(), attributes: $inputAttributes); if ($this->uncheckValue !== null) { $tag = $tag->uncheckValue($this->uncheckValue); if (!empty($this->uncheckInputAttributes)) { diff --git a/src/Field/Hidden.php b/src/Field/Hidden.php index 34a7089d..4c493f55 100644 --- a/src/Field/Hidden.php +++ b/src/Field/Hidden.php @@ -24,7 +24,7 @@ final class Hidden extends InputField protected function generateInput(): string { - $value = $this->getInputData()->getValue(); + $value = $this->getValue(); if (!is_string($value) && !is_numeric($value) && $value !== null) { throw new InvalidArgumentException('Hidden widget requires a string, numeric or null value.'); @@ -32,6 +32,6 @@ protected function generateInput(): string $inputAttributes = $this->getInputAttributes(); - return Html::hiddenInput($this->getInputData()->getName(), $value, $inputAttributes)->render(); + return Html::hiddenInput($this->getName(), $value, $inputAttributes)->render(); } } diff --git a/src/Field/Number.php b/src/Field/Number.php index 6f9f00e7..59d4e51a 100644 --- a/src/Field/Number.php +++ b/src/Field/Number.php @@ -191,7 +191,7 @@ protected function beforeRender(): void protected function generateInput(): string { - $value = $this->getInputData()->getValue(); + $value = $this->getValue(); if (!is_numeric($value) && $value !== null) { throw new InvalidArgumentException('Number field requires a numeric or null value.'); @@ -199,7 +199,7 @@ protected function generateInput(): string $inputAttributes = $this->getInputAttributes(); - return Html::input('number', $this->getInputData()->getName(), $value, $inputAttributes)->render(); + return Html::input('number', $this->getName(), $value, $inputAttributes)->render(); } protected function prepareContainerAttributes(array &$attributes): void diff --git a/src/Field/Password.php b/src/Field/Password.php index 2fc5ffbc..e584e092 100644 --- a/src/Field/Password.php +++ b/src/Field/Password.php @@ -230,7 +230,7 @@ protected function beforeRender(): void protected function generateInput(): string { - $value = $this->getInputData()->getValue(); + $value = $this->getValue(); if (!is_string($value) && $value !== null) { throw new InvalidArgumentException('Password field requires a string or null value.'); @@ -238,7 +238,7 @@ protected function generateInput(): string $inputAttributes = $this->getInputAttributes(); - return Html::passwordInput($this->getInputData()->getName(), $value, $inputAttributes)->render(); + return Html::passwordInput($this->getName(), $value, $inputAttributes)->render(); } protected function prepareContainerAttributes(array &$attributes): void diff --git a/src/Field/RadioList.php b/src/Field/RadioList.php index 2a78fdf5..1241a494 100644 --- a/src/Field/RadioList.php +++ b/src/Field/RadioList.php @@ -8,7 +8,7 @@ use InvalidArgumentException; use LogicException; use Stringable; -use Yiisoft\Form\Field\Base\InputData\CustomNameAndValueForInputDataTrait; +use Yiisoft\Form\Field\Base\InputData\InputDataWithCustomNameAndValueTrait; use Yiisoft\Form\Field\Base\InputData\InputDataTrait; use Yiisoft\Form\Field\Base\PartsField; use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassInterface; @@ -24,8 +24,7 @@ */ final class RadioList extends PartsField implements ValidationClassInterface { - use CustomNameAndValueForInputDataTrait; - use InputDataTrait; + use InputDataWithCustomNameAndValueTrait; use ValidationClassTrait; private RadioListWidget $widget; @@ -144,12 +143,12 @@ public function itemFormatter(?Closure $formatter): self protected function generateInput(): string { - $name = $this->getInputData()->getName(); + $name = $this->getName(); if (empty($name)) { throw new LogicException('"RadioList" field requires non-empty name.'); } - $value = $this->getInputData()->getValue(); + $value = $this->getValue(); if (!is_bool($value) && !is_string($value) diff --git a/src/Field/Range.php b/src/Field/Range.php index 0cd21d3f..cdec45c2 100644 --- a/src/Field/Range.php +++ b/src/Field/Range.php @@ -219,13 +219,13 @@ protected function beforeRender(): void protected function generateInput(): string { - $value = $this->getInputData()->getValue(); + $value = $this->getValue(); if (!is_string($value) && !is_numeric($value) && $value !== null) { throw new InvalidArgumentException('Range field requires a string, numeric or null value.'); } - $tag = Html::range($this->getInputData()->getName(), $value, $this->getInputAttributes()); + $tag = Html::range($this->getName(), $value, $this->getInputAttributes()); if ($this->showOutput) { $tag = $tag ->showOutput() diff --git a/src/Field/Select.php b/src/Field/Select.php index 0f653b6d..6f620fa0 100644 --- a/src/Field/Select.php +++ b/src/Field/Select.php @@ -260,7 +260,7 @@ protected function beforeRender(): void protected function generateInput(): string { - $value = $this->getInputData()->getValue(); + $value = $this->getValue(); $multiple = (bool) ($this->inputAttributes['multiple'] ?? false); if ($multiple) { @@ -290,7 +290,7 @@ protected function generateInput(): string return $this->select ->addAttributes($selectAttributes) - ->name($this->getInputData()->getName()) + ->name($this->getName()) ->values($value) ->render(); } diff --git a/src/Field/Telephone.php b/src/Field/Telephone.php index 91fef7d5..acb13eed 100644 --- a/src/Field/Telephone.php +++ b/src/Field/Telephone.php @@ -225,7 +225,7 @@ protected function beforeRender(): void protected function generateInput(): string { - $value = $this->getInputData()->getValue(); + $value = $this->getValue(); if (!is_string($value) && $value !== null) { throw new InvalidArgumentException('Telephone field requires a string or null value.'); @@ -233,7 +233,7 @@ protected function generateInput(): string $inputAttributes = $this->getInputAttributes(); - return Html::input('tel', $this->getInputData()->getName(), $value, $inputAttributes)->render(); + return Html::input('tel', $this->getName(), $value, $inputAttributes)->render(); } protected function prepareContainerAttributes(array &$attributes): void diff --git a/src/Field/Text.php b/src/Field/Text.php index 922953f5..a16b4717 100644 --- a/src/Field/Text.php +++ b/src/Field/Text.php @@ -241,7 +241,7 @@ protected function beforeRender(): void protected function generateInput(): string { - $value = $this->getInputData()->getValue(); + $value = $this->getValue(); if (!is_string($value) && $value !== null) { throw new InvalidArgumentException('Text field requires a string or null value.'); @@ -249,7 +249,7 @@ protected function generateInput(): string $inputAttributes = $this->getInputAttributes(); - return Html::textInput($this->getInputData()->getName(), $value, $inputAttributes)->render(); + return Html::textInput($this->getName(), $value, $inputAttributes)->render(); } protected function prepareContainerAttributes(array &$attributes): void diff --git a/src/Field/Textarea.php b/src/Field/Textarea.php index ef669139..85ffbbab 100644 --- a/src/Field/Textarea.php +++ b/src/Field/Textarea.php @@ -247,7 +247,7 @@ protected function beforeRender(): void protected function generateInput(): string { - $value = $this->getInputData()->getValue(); + $value = $this->getValue(); if (!is_string($value) && $value !== null) { throw new InvalidArgumentException('Textarea field requires a string or null value.'); @@ -255,7 +255,7 @@ protected function generateInput(): string $textareaAttributes = $this->getInputAttributes(); - return Html::textarea($this->getInputData()->getName(), $value, $textareaAttributes)->render(); + return Html::textarea($this->getName(), $value, $textareaAttributes)->render(); } protected function prepareContainerAttributes(array &$attributes): void diff --git a/src/Field/Url.php b/src/Field/Url.php index 2a5cda5e..552a199c 100644 --- a/src/Field/Url.php +++ b/src/Field/Url.php @@ -231,7 +231,7 @@ protected function beforeRender(): void protected function generateInput(): string { - $value = $this->getInputData()->getValue(); + $value = $this->getValue(); if (!is_string($value) && $value !== null) { throw new InvalidArgumentException('URL field requires a string or null value.'); @@ -239,7 +239,7 @@ protected function generateInput(): string $inputAttributes = $this->getInputAttributes(); - return Html::input('url', $this->getInputData()->getName(), $value, $inputAttributes)->render(); + return Html::input('url', $this->getName(), $value, $inputAttributes)->render(); } protected function prepareContainerAttributes(array &$attributes): void diff --git a/tests/Field/TextTest.php b/tests/Field/TextTest.php index e91225b5..5af94d54 100644 --- a/tests/Field/TextTest.php +++ b/tests/Field/TextTest.php @@ -71,8 +71,8 @@ public function testCustomNameAfterInputData(): void { $expected = << - - + +
Input your full name.
Value cannot be blank.
From 14ba730e78ff9cbe77166b7ecbc356ca9a6c3de3 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Sun, 26 Nov 2023 07:55:33 +0000 Subject: [PATCH 4/4] Apply fixes from StyleCI --- src/Field/Base/InputField.php | 1 - src/Field/CheckboxList.php | 1 - src/Field/RadioList.php | 1 - 3 files changed, 3 deletions(-) diff --git a/src/Field/Base/InputField.php b/src/Field/Base/InputField.php index 548774dc..5d7f8d84 100644 --- a/src/Field/Base/InputField.php +++ b/src/Field/Base/InputField.php @@ -5,7 +5,6 @@ namespace Yiisoft\Form\Field\Base; use Yiisoft\Form\Field\Base\InputData\InputDataWithCustomNameAndValueTrait; -use Yiisoft\Form\Field\Base\InputData\InputDataTrait; use Yiisoft\Form\Field\Part\Error; use Yiisoft\Form\Field\Part\Hint; use Yiisoft\Form\Field\Part\Label; diff --git a/src/Field/CheckboxList.php b/src/Field/CheckboxList.php index b33d03d1..9bf677eb 100644 --- a/src/Field/CheckboxList.php +++ b/src/Field/CheckboxList.php @@ -9,7 +9,6 @@ use LogicException; use Stringable; use Yiisoft\Form\Field\Base\InputData\InputDataWithCustomNameAndValueTrait; -use Yiisoft\Form\Field\Base\InputData\InputDataTrait; use Yiisoft\Form\Field\Base\PartsField; use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassInterface; use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassTrait; diff --git a/src/Field/RadioList.php b/src/Field/RadioList.php index 1241a494..a232ca89 100644 --- a/src/Field/RadioList.php +++ b/src/Field/RadioList.php @@ -9,7 +9,6 @@ use LogicException; use Stringable; use Yiisoft\Form\Field\Base\InputData\InputDataWithCustomNameAndValueTrait; -use Yiisoft\Form\Field\Base\InputData\InputDataTrait; use Yiisoft\Form\Field\Base\PartsField; use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassInterface; use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassTrait;