diff --git a/.github/workflows/phpstan.yaml b/.github/workflows/phpstan.yaml index 2a707a2..6e1c838 100644 --- a/.github/workflows/phpstan.yaml +++ b/.github/workflows/phpstan.yaml @@ -7,7 +7,6 @@ on: jobs: phpstan: - name: phpstan runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 diff --git a/src/BaseItem.php b/src/BaseItem.php index b6411b9..f67df30 100644 --- a/src/BaseItem.php +++ b/src/BaseItem.php @@ -114,10 +114,6 @@ public function value(mixed $value = null): static $value = $value->value; } - if (app('request')->old($this->attributes->getAttribute('name'))) { - $value = app('request')->old($this->attributes->getAttribute('name')); - } - $this->attributes->setAttribute('value', $value); return $this; @@ -134,10 +130,6 @@ public function setValue(mixed $value = null): static $value = $value->value; } - if (app('request')->old($this->attributes->getAttribute('name'))) { - $value = app('request')->old($this->attributes->getAttribute('name')); - } - $this->attributes->setAttribute('value', $value); return $this; @@ -236,6 +228,8 @@ public function toHtml(): string $this->attributes->addClass(config('laravel-form.errors.element-class', 'is-invalid')); } + $this->setOld(); + return new HtmlString( match ($this->viewName) { 'input' => (new InputRender())->render($this->attributes), @@ -291,4 +285,18 @@ private function getErrors(): string return $errors; } + + private function setOld(): void + { + $value = $this->attributes->getAttribute('value'); + + if (!$this->attributes->getAttribute('name')) { + return; + } + + $this->attributes->setAttribute( + name: 'value', + value: app('session')->getOldInput($this->attributes->getAttribute('name'), $value) + ); + } } diff --git a/tests/FormInputTest.php b/tests/FormInputTest.php index 286c290..5ab51be 100644 --- a/tests/FormInputTest.php +++ b/tests/FormInputTest.php @@ -117,4 +117,26 @@ public function testCanCreateInputWithoutRequired(): void $this->assertEquals('', $item); } + + public function testCanCreateInputWithOldValues() + { + $this->setInputs(); + + $item = LaravelForm::input()->name('test'); + + $this->assertEquals('', $item); + } + + public function testCanCreateInputWithOldValuesAndErrors() + { + $this->setInputs(); + $this->setErrors(); + + $item = LaravelForm::input()->name('test'); + + $this->assertEquals( + expected: 'error', + actual: $item + ); + } } diff --git a/tests/FormTextareaTest.php b/tests/FormTextareaTest.php index bfdd8df..6523a43 100644 --- a/tests/FormTextareaTest.php +++ b/tests/FormTextareaTest.php @@ -55,8 +55,17 @@ public function testCanCreateTextareaWithErrors(): void public function testCanCreateTextareaWithValue(): void { - $item = LaravelForm::textarea()->value('test'); + $item = LaravelForm::textarea()->value('test')->toHtml(); $this->assertEquals('', $item); } + + public function testCanCreateTextareaWithOldValue(): void + { + $this->setInputs(); + + $item = LaravelForm::textarea()->name('test'); + + $this->assertEquals('', $item); + } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 70ee8bc..5c57b1e 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -24,4 +24,10 @@ public function setErrors(): void $this->startSession(); $this->withSession(['errors' => (new ViewErrorBag)->put('default', new MessageBag(['test' => 'error']))]); } + + public function setInputs(): void + { + $this->startSession(); + app('session')->flashInput(['test' => 'test']); + } }